Skip to content

Commit d1b45f3

Browse files
author
Tim Foley
authored
IR: support for select and negate (shader-slang#257)
- During IR emit, treat a "select" expression (`?:` operator) like any other `InvokeExpr`, since it will have an `__intrinsic_op` modifier attached to turn it into a `select` instruction. - During HLSL/GLSL emit from IR, turn a `select` instruction into a `?:` expression - Also add support for the `neg` instruction during HLSL/GLSL emit Note that right now we are assuming HLSL semantics for `?:` where it does not short-circuit. Correctly handling the GLSL case would require going back to special-case codegen for `SelectExpr`, but we can cross that bridge when we come to it.
1 parent 9919c82 commit d1b45f3

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

source/slang/emit.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -4911,6 +4911,13 @@ emitDeclImpl(decl, nullptr);
49114911
}
49124912
break;
49134913

4914+
case kIROp_Neg:
4915+
{
4916+
emit("-");
4917+
emitIROperand(ctx, inst->getArg(0));
4918+
}
4919+
break;
4920+
49144921
case kIROp_Sample:
49154922
emitIROperand(ctx, inst->getArg(0));
49164923
emit(".Sample(");
@@ -5033,6 +5040,16 @@ emitDeclImpl(decl, nullptr);
50335040
}
50345041
break;
50355042

5043+
case kIROp_Select:
5044+
{
5045+
emitIROperand(ctx, inst->getArg(0));
5046+
emit(" ? ");
5047+
emitIROperand(ctx, inst->getArg(1));
5048+
emit(" : ");
5049+
emitIROperand(ctx, inst->getArg(2));
5050+
}
5051+
break;
5052+
50365053
default:
50375054
emit("/* unhandled */");
50385055
break;

source/slang/lower-to-ir.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -1497,11 +1497,6 @@ struct ExprLoweringVisitorBase : ExprVisitor<Derived, LoweredValInfo>
14971497
return emitDeclRef(context, expr->declRef);
14981498
}
14991499

1500-
LoweredValInfo visitSelectExpr(SelectExpr* /*expr*/)
1501-
{
1502-
SLANG_UNIMPLEMENTED_X("codegen for select expression");
1503-
}
1504-
15051500
LoweredValInfo visitGenericAppExpr(GenericAppExpr* /*expr*/)
15061501
{
15071502
SLANG_UNIMPLEMENTED_X("generic application expression during code generation");

tests/compute/select-expr.slang

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//TEST(smoke,compute):COMPARE_COMPUTE:
2+
//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):dxbinding(0),glbinding(0),out
3+
4+
5+
// Test IR code generation for the `?:` "select" operator
6+
7+
int test(int input)
8+
{
9+
return input > 1 ? -input : input;
10+
}
11+
12+
RWStructuredBuffer<int> outputBuffer;
13+
14+
[numthreads(4, 1, 1)]
15+
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
16+
{
17+
outputBuffer[dispatchThreadID.x] = test((int) dispatchThreadID.x);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
FFFFFFFE
4+
FFFFFFFD

0 commit comments

Comments
 (0)