Browse Source

[spirv] Support CXXDefaultArgExpr translation. (#825)

Ehsan 7 years ago
parent
commit
3809395c08

+ 4 - 0
tools/clang/lib/SPIRV/SPIRVEmitter.cpp

@@ -509,6 +509,10 @@ SpirvEvalInfo SPIRVEmitter::doExpr(const Expr *expr) {
     return doConditionalOperator(condExpr);
   }
 
+  if (const auto *defaultArgExpr = dyn_cast<CXXDefaultArgExpr>(expr)) {
+    return doExpr(defaultArgExpr->getParam()->getDefaultArg());
+  }
+
   if (isa<CXXThisExpr>(expr)) {
     assert(curThis);
     return curThis;

+ 28 - 0
tools/clang/test/CodeGenSPIRV/fn.default-arg.hlsl

@@ -0,0 +1,28 @@
+// Run: %dxc -T vs_6_0 -E main
+
+int foo(int x = 5)
+{
+  return x + 1;
+}
+
+void main()
+{
+// CHECK:      %param_var_x = OpVariable %_ptr_Function_int Function
+// CHECK-NEXT: %param_var_x_0 = OpVariable %_ptr_Function_int Function
+// CHECK-NEXT: %param_var_x_1 = OpVariable %_ptr_Function_int Function
+
+// CHECK-NEXT: OpStore %param_var_x %int_5
+// CHECK-NEXT: {{%\d+}} = OpFunctionCall %int %foo %param_var_x
+  // Call without passing arg.
+  foo();
+
+// CHECK-NEXT: OpStore %param_var_x_0 %int_2
+// CHECK-NEXT: {{%\d+}} = OpFunctionCall %int %foo %param_var_x_0
+  // Call with passing arg.
+  foo(2);
+
+// CHECK-NEXT: OpStore %param_var_x_1 %int_5
+// CHECK-NEXT: {{%\d+}} = OpFunctionCall %int %foo %param_var_x_1
+  // Call without passing arg again.
+  foo();
+}

+ 1 - 0
tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp

@@ -354,6 +354,7 @@ TEST_F(FileTest, ControlFlowConditionalOp) { runFileTest("cf.cond-op.hlsl"); }
 
 // For functions
 TEST_F(FileTest, FunctionCall) { runFileTest("fn.call.hlsl"); }
+TEST_F(FileTest, FunctionDefaultArg) { runFileTest("fn.default-arg.hlsl"); }
 TEST_F(FileTest, FunctionInOutParam) { runFileTest("fn.param.inout.hlsl"); }
 
 // For OO features