Browse Source

Use undef to init ret value. (#2974)

Xiang Li 5 years ago
parent
commit
a509c34726

+ 9 - 1
tools/clang/lib/CodeGen/CGHLSLMSFinishCodeGen.cpp

@@ -2789,6 +2789,14 @@ void updateEndScope(
   EndBBToScopeIndexMap[newEndScope] = scopeList;
 }
 
+// Init ret value with undef to make sure it will not live thru loop inside
+// callers.
+// Because structurize return, the flow is controled by bIsReturned. The
+// semantic is the same as multiple return, but without konwledge of
+// bIsReturend, some path for structrized flow will have ret value not
+// initialized.
+// When function is called inside loop, ret value will live across the loop
+// after inline.
 void InitRetValue(BasicBlock *exitBB) {
   Value *RetValPtr = nullptr;
   if (ReturnInst *RI = dyn_cast<ReturnInst>(exitBB->getTerminator())) {
@@ -2803,7 +2811,7 @@ void InitRetValue(BasicBlock *exitBB) {
   if (AllocaInst *RetVAlloc = dyn_cast<AllocaInst>(RetValPtr)) {
     IRBuilder<> B(RetVAlloc->getNextNode());
     Type *Ty = RetVAlloc->getAllocatedType();
-    Value *Init = Constant::getNullValue(Ty);
+    Value *Init = UndefValue::get(Ty);
     if (Ty->isAggregateType()) {
       // TODO: support aggreagate type and out parameters.
       // Skip it here will cause undef on phi which the incoming path should never hit.

+ 2 - 2
tools/clang/test/HLSLFileCheck/hlsl/control_flow/return/whole_scope_returned_if.hlsl

@@ -5,8 +5,8 @@ float main(float4 a:A) : SV_Target {
 // CHECK:%[[bReturned:.*]] = alloca i1
 // CHECK:store i1 false, i1* %[[bReturned]]
 
-// Init retVal to 0.
-// CHECK:store float 0.000000e+00
+// Init retVal to undef.
+// CHECK:store float undef
 
 // CHECK: [[label:.*]] ; preds =
   if (a.w < 0) {

+ 1 - 1
tools/clang/test/HLSLFileCheck/hlsl/control_flow/return/whole_scope_returned_loop.hlsl

@@ -7,7 +7,7 @@ float main(float4 a:A) : SV_Target {
 // CHECK:%[[bReturned:.*]] = alloca i1
 // CHECK-NEXT:store i1 false, i1* %[[bReturned]]
 // Init retVal to 0.
-// CHECK:store float 0.000000e+00
+// CHECK:store float undef
   float r = a.w;
 
 // CHECK: [[if_then:.*]] ; preds =