From 3809395c080a8b2aa4e3ddcd3998c04798f859d0 Mon Sep 17 00:00:00 2001 From: Ehsan Date: Mon, 20 Nov 2017 08:46:07 -0500 Subject: [PATCH] [spirv] Support CXXDefaultArgExpr translation. (#825) --- tools/clang/lib/SPIRV/SPIRVEmitter.cpp | 4 +++ .../test/CodeGenSPIRV/fn.default-arg.hlsl | 28 +++++++++++++++++++ .../unittests/SPIRV/CodeGenSPIRVTest.cpp | 1 + 3 files changed, 33 insertions(+) create mode 100644 tools/clang/test/CodeGenSPIRV/fn.default-arg.hlsl diff --git a/tools/clang/lib/SPIRV/SPIRVEmitter.cpp b/tools/clang/lib/SPIRV/SPIRVEmitter.cpp index b54a68f9d..f0bfbcbb6 100644 --- a/tools/clang/lib/SPIRV/SPIRVEmitter.cpp +++ b/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(expr)) { + return doExpr(defaultArgExpr->getParam()->getDefaultArg()); + } + if (isa(expr)) { assert(curThis); return curThis; diff --git a/tools/clang/test/CodeGenSPIRV/fn.default-arg.hlsl b/tools/clang/test/CodeGenSPIRV/fn.default-arg.hlsl new file mode 100644 index 000000000..5b8d42b78 --- /dev/null +++ b/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(); +} diff --git a/tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp b/tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp index 02cef823d..1aab18993 100644 --- a/tools/clang/unittests/SPIRV/CodeGenSPIRVTest.cpp +++ b/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