diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 2a32c8e89d..6bb1fdb950 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -904,7 +904,7 @@ uint32_t Builder::GenerateCallExpression(ast::CallExpression* expr) { if (id == 0) { return 0; } - ops.push_back(Operand::Int(id)); + ops.push_back(Operand::Int(GenerateLoadIfNeeded(param->result_type(), id))); } push_function_inst(spv::Op::OpExtInst, std::move(ops)); diff --git a/src/writer/spirv/builder_call_test.cc b/src/writer/spirv/builder_call_test.cc index 7fef65a1f9..ea4dd9533a 100644 --- a/src/writer/spirv/builder_call_test.cc +++ b/src/writer/spirv/builder_call_test.cc @@ -75,6 +75,55 @@ OpFunctionEnd )"); } +TEST_F(BuilderTest, Call_GLSLMethod_WithLoad) { + ast::type::F32Type f32; + ast::type::VoidType void_type; + + auto var = std::make_unique("ident", + ast::StorageClass::kPrivate, &f32); + + ast::ExpressionList params; + params.push_back(std::make_unique("ident")); + + ast::CallExpression expr(std::make_unique( + std::vector{"std", "round"}), + std::move(params)); + + Context ctx; + ast::Module mod; + TypeDeterminer td(&ctx, &mod); + td.RegisterVariableForTesting(var.get()); + + auto imp = std::make_unique("GLSL.std.450", "std"); + auto* glsl = imp.get(); + mod.AddImport(std::move(imp)); + + ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error(); + + ast::Function func("a_func", {}, &void_type); + + Builder b(&mod); + b.GenerateImport(glsl); + ASSERT_TRUE(b.GenerateGlobalVariable(var.get())) << b.error(); + ASSERT_TRUE(b.GenerateFunction(&func)) << b.error(); + + EXPECT_EQ(b.GenerateCallExpression(&expr), 9u) << b.error(); + EXPECT_EQ(DumpBuilder(b), R"(%1 = OpExtInstImport "GLSL.std.450" +OpName %2 "ident" +OpName %7 "a_func" +%4 = OpTypeFloat 32 +%3 = OpTypePointer Private %4 +%2 = OpVariable %3 Private +%6 = OpTypeVoid +%5 = OpTypeFunction %6 +%7 = OpFunction %6 None %5 +%8 = OpLabel +%10 = OpLoad %4 %2 +%9 = OpExtInst %4 %1 Round %10 +OpFunctionEnd +)"); +} + } // namespace } // namespace spirv } // namespace writer