Skip to content

Commit 314795b

Browse files
author
Tim Foley
authored
Fix confused definitions of pre/post increment/decrement (shader-slang#827)
* Fix confused definitions of pre/post increment/decrement We somehow have been compiling `++i` as `i++` (and vice versa) for a long time without noticing. This change fixes the implementation of these pseudo-ops in IR codegen, and adds a comment to explain the rationale for why their definitions should be what they are. * fixup: typo
1 parent a17b68a commit 314795b

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

source/slang/lower-to-ir.cpp

+16-6
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ IRInst* getOneValOfType(
534534
UNREACHABLE_RETURN(nullptr);
535535
}
536536

537-
LoweredValInfo emitPreOp(
537+
LoweredValInfo emitPrefixIncDecOp(
538538
IRGenContext* context,
539539
IRType* type,
540540
IROp op,
@@ -555,10 +555,15 @@ LoweredValInfo emitPreOp(
555555

556556
builder->emitStore(argPtr, innerOp);
557557

558-
return LoweredValInfo::simple(preVal);
558+
// For a prefix operator like `++i` we return
559+
// the value after the increment/decrement has
560+
// been applied. In casual terms we "increment
561+
// the varaible, then return its value."
562+
//
563+
return LoweredValInfo::simple(innerOp);
559564
}
560565

561-
LoweredValInfo emitPostOp(
566+
LoweredValInfo emitPostfixIncDecOp(
562567
IRGenContext* context,
563568
IRType* type,
564569
IROp op,
@@ -579,7 +584,12 @@ LoweredValInfo emitPostOp(
579584

580585
builder->emitStore(argPtr, innerOp);
581586

582-
return LoweredValInfo::ptr(argPtr);
587+
// For a postfix operator like `i++` we return
588+
// the value that we read before the increment/decrement
589+
// gets applied. In casual terms we "read
590+
// the variable, then increment it."
591+
//
592+
return LoweredValInfo::simple(preVal);
583593
}
584594

585595
LoweredValInfo lowerRValueExpr(
@@ -707,13 +717,13 @@ LoweredValInfo emitCallToDeclRef(
707717
#undef CASE
708718

709719
#define CASE(COMPOUND, OP) \
710-
case COMPOUND: return emitPreOp(context, type, OP, argCount, args)
720+
case COMPOUND: return emitPrefixIncDecOp(context, type, OP, argCount, args)
711721
CASE(kIRPseudoOp_PreInc, kIROp_Add);
712722
CASE(kIRPseudoOp_PreDec, kIROp_Sub);
713723
#undef CASE
714724

715725
#define CASE(COMPOUND, OP) \
716-
case COMPOUND: return emitPostOp(context, type, OP, argCount, args)
726+
case COMPOUND: return emitPostfixIncDecOp(context, type, OP, argCount, args)
717727
CASE(kIRPseudoOp_PostInc, kIROp_Add);
718728
CASE(kIRPseudoOp_PostDec, kIROp_Sub);
719729
#undef CASE

0 commit comments

Comments
 (0)