[clang] [llvm] [HLSL] Adding Flatten and Branch if attributes (PR #116331)

via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 16 09:58:22 PST 2024


================
@@ -2076,7 +2076,28 @@ void CodeGenFunction::EmitBranchOnBoolExpr(
     Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount);
   }
 
-  Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable);
+  auto *BrInst = Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights,
+                                      Unpredictable);
+  switch (HLSLControlFlowAttr) {
+  case HLSLControlFlowHintAttr::Microsoft_branch:
+  case HLSLControlFlowHintAttr::Microsoft_flatten: {
+    llvm::MDBuilder MDHelper(CGM.getLLVMContext());
+
+    llvm::ConstantInt *BranchHintConstant =
+        HLSLControlFlowAttr ==
+                HLSLControlFlowHintAttr::Spelling::Microsoft_branch
+            ? llvm::ConstantInt::get(CGM.Int32Ty, 1)
+            : llvm::ConstantInt::get(CGM.Int32Ty, 2);
+
+    SmallVector<llvm::Metadata *, 2> Vals(
+        {MDHelper.createString("hlsl.controlflow.hint"),
+         MDHelper.createConstant(BranchHintConstant)});
+    BrInst->setMetadata("hlsl.controlflow.hint",
+                        llvm::MDNode::get(CGM.getLLVMContext(), Vals));
+  } break;
+  case HLSLControlFlowHintAttr::SpellingNotCalculated:
----------------
joaosaffran wrote:

`SpellingNotCalculated` is the default value for `HLSLControlFlowAttr`. I noticed that this case doesn't make sense to exist, so I will remove it 

https://github.com/llvm/llvm-project/pull/116331


More information about the llvm-commits mailing list