Skip to content

Commit 9f09f6e

Browse files
committed
Add a test case for GetElement(FieldExtract(...), GetElement(...))
Update the commentary in transformation code
1 parent f193dea commit 9f09f6e

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

source/slang/slang-ir-transform-params-to-constref.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ struct TransformParamsToConstRefContext
5353
// - Scan transitively the uses of the transformed ops, and add IRLoad
5454
// in front of them as well.
5555
//
56-
// Phase 2 (post-fixup)
56+
// Phase 2
5757
// - Check all translated IRGetElementPtr instructions. If the index is
5858
// a newly translated IRGetElementPtr or IRFieldAddress, add an IRLoad
59-
// to fetch the value of the pointer. This load was omitted in Phase 1
60-
// due to the exception.
61-
// - Note that the post-fixup phase avoids problems with rewrite
62-
// ordering, so we don't try to fuse this in Phase 1
59+
// to fetch the value of the pointer. This insertion of load may have
60+
// been omitted in Phase 1 due to the exception.
61+
// - Note that doing a separate phase avoids problems with IR
62+
// op rewrite ordering, since all rewrites are already done in Phase
6363
//
64-
// In addition, IRStore instructions are attempted to also optimize. See
65-
// the related comment below.
64+
// In addition, IRStore instructions are also attempted to be
65+
// optimized. See the related comment below.
6666

6767
HashSet<IRInst*> workListSet;
6868
workListSet.add(newAddrInst);
@@ -152,17 +152,17 @@ struct TransformParamsToConstRefContext
152152
auto getElementPtr = as<IRGetElementPtr>(inst);
153153
IRInst* indexInst = getElementPtr->getOperand(1);
154154

155-
// In the postfix phase, we'll only consider index instructions if
156-
// they were transformed previously by this function. This is an
157-
// extra precaution to avoid hiding bugs by other passes, should
158-
// they also pass pointers directly as the index.
155+
// We'll only consider index instructions if they were transformed
156+
// previously by this function. This is an extra precaution to avoid
157+
// hiding bugs by other passes, should they also pass pointers
158+
// directly as the IRGetElementPtr index.
159159
if (workListSet.contains(indexInst))
160160
{
161161
switch (indexInst->getOp())
162162
{
163163
case kIROp_FieldAddress:
164164
case kIROp_GetElementPtr:
165-
// post-fix needed
165+
// additional IRLoad() needed
166166
builder.setInsertBefore(getElementPtr);
167167
auto loadInst = builder.emitLoad(indexInst);
168168
getElementPtr->setOperand(1, loadInst);

tests/bugs/gh-9073-spirv-pointer-double-dereference.slang

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,47 @@ struct Payload
99

1010
int GetVolume()
1111
{
12+
// This function exercises the following transformation in
13+
// transformParamsToConstRef():
14+
//
15+
// IRGetElement(
16+
// IRFieldExtract(...),
17+
// IRFieldExtract(...))
18+
//
19+
// =>
20+
//
21+
// IRGetElementPtr(
22+
// IRFieldAddress(...),
23+
// IRLoad(IRFieldAddress(...)))
24+
//
25+
// That is, that IRLoad() is correctly injected to
26+
// IRGetElementPtr() index when IRFieldExtract() is
27+
// transformed to IRFieldAddress()
28+
1229
return volumeStack[volumeStackIndex];
1330
}
31+
32+
int GetVolume2()
33+
{
34+
// This function exercises the following transformation in
35+
// transformParamsToConstRef():
36+
//
37+
// IRGetElement(
38+
// IRFieldExtract(...),
39+
// IRGetElement(...))
40+
//
41+
// =>
42+
//
43+
// IRGetElementPtr(
44+
// IRFieldAddress(...),
45+
// IRLoad(IRGetElementPtr(...)))
46+
//
47+
// That is, that IRLoad() is correctly injected to
48+
// IRGetElementPtr() index when IRGetElement() is
49+
// transformed to IRGetElementPtr()
50+
51+
return volumeStack[volumeStack[volumeStackIndex]];
52+
}
1453
}
1554

1655
RWStructuredBuffer<int> outputBuffer;
@@ -23,4 +62,6 @@ void main(inout Payload payload)
2362
// POSITIVE-NOT: {{(error|warning).*}}:
2463

2564
outputBuffer[0] = payload.GetVolume();
65+
66+
outputBuffer[1] = payload.GetVolume2();
2667
}

0 commit comments

Comments
 (0)