2
0
Эх сурвалжийг харах

Use metadata for precise on function call. (#51)

Xiang Li 8 жил өмнө
parent
commit
54bb69d607

+ 3 - 0
lib/HLSL/DxilGenerationPass.cpp

@@ -2724,6 +2724,9 @@ static void PropagatePreciseAttributeOnOperand(Value *V, DxilTypeSystem &typeSys
 
   // Clear fast math.
   I->copyFastMathFlags(FastMathFlags());
+  // Fast math not work on call, use metadata.
+  if (CallInst *CI = dyn_cast<CallInst>(I))
+    HLModule::MarkPreciseAttributeWithMetadata(CI);
   PropagatePreciseAttribute(I, typeSys);
 }
 

+ 2 - 1
lib/HLSL/HLModule.cpp

@@ -985,7 +985,8 @@ bool HLModule::HasPreciseAttributeWithMetadata(Instruction *I) {
 void HLModule::MarkPreciseAttributeWithMetadata(Instruction *I) {
   LLVMContext &Ctx = I->getContext();
   MDNode *preciseNode = MDNode::get(
-      Ctx, {MDString::get(Ctx, DxilMDHelper::kDxilPreciseAttributeMDName)});
+      Ctx,
+      {ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Ctx), 1))});
 
   I->setMetadata(DxilMDHelper::kDxilPreciseAttributeMDName, preciseNode);
 }

+ 10 - 0
tools/clang/test/CodeGenHLSL/precise_call.hlsl

@@ -0,0 +1,10 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// CHECK: !dx.precise
+
+float4 main(float a : A) : SV_Target
+{
+  precise float psin = sin(a);
+
+  return psin;
+}

+ 10 - 0
tools/clang/test/CodeGenHLSL/precise_call_not.hlsl

@@ -0,0 +1,10 @@
+// RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
+
+// CHECK-NOT: !dx.precise
+
+float4 main(float a : A) : SV_Target
+{
+  float psin = sin(a);
+
+  return psin;
+}

+ 10 - 0
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -429,6 +429,8 @@ public:
   TEST_METHOD(CodeGenPrecise2)
   TEST_METHOD(CodeGenPrecise3)
   TEST_METHOD(CodeGenPrecise4)
+  TEST_METHOD(CodeGenPreciseOnCall)
+  TEST_METHOD(CodeGenPreciseOnCallNot)
   TEST_METHOD(CodeGenRaw_Buf1)
   TEST_METHOD(CodeGenRcp1)
   TEST_METHOD(CodeGenReadFromOutput)
@@ -2218,6 +2220,14 @@ TEST_F(CompilerTest, CodeGenPrecise4) {
   CodeGenTestCheck(L"..\\CodeGenHLSL\\precise4.hlsl");
 }
 
+TEST_F(CompilerTest, CodeGenPreciseOnCall) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\precise_call.hlsl");
+}
+
+TEST_F(CompilerTest, CodeGenPreciseOnCallNot) {
+  CodeGenTestCheck(L"..\\CodeGenHLSL\\precise_call_not.hlsl");
+}
+
 TEST_F(CompilerTest, CodeGenRaw_Buf1) {
   CodeGenTest(L"..\\CodeGenHLSL\\raw_buf1.hlsl");
 }