[llvm] [NFC][DirectX] Refactor `DXILPrepare`/`DXILTranslateMetadata` (PR #164285)

Helena Kotas via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 21 12:21:02 PDT 2025


================
@@ -290,42 +292,84 @@ static MDTuple *emitTopLevelLibraryNode(Module &M, MDNode *RMD,
   return constructEntryMetadata(nullptr, nullptr, RMD, Properties, Ctx);
 }
 
-// TODO: We might need to refactor this to be more generic,
-// in case we need more metadata to be replaced.
-static void translateBranchMetadata(Module &M) {
-  for (Function &F : M) {
-    for (BasicBlock &BB : F) {
-      Instruction *BBTerminatorInst = BB.getTerminator();
+static void translateBranchMetadata(Module &M, Instruction *BBTerminatorInst) {
+  MDNode *HlslControlFlowMD =
+      BBTerminatorInst->getMetadata("hlsl.controlflow.hint");
+
+  if (!HlslControlFlowMD)
+    return;
 
-      MDNode *HlslControlFlowMD =
-          BBTerminatorInst->getMetadata("hlsl.controlflow.hint");
+  assert(HlslControlFlowMD->getNumOperands() == 2 &&
+         "invalid operands for hlsl.controlflow.hint");
 
-      if (!HlslControlFlowMD)
-        continue;
+  MDBuilder MDHelper(M.getContext());
+  ConstantInt *Op1 =
+      mdconst::extract<ConstantInt>(HlslControlFlowMD->getOperand(1));
 
-      assert(HlslControlFlowMD->getNumOperands() == 2 &&
-             "invalid operands for hlsl.controlflow.hint");
+  SmallVector<llvm::Metadata *, 2> Vals(
+      ArrayRef<Metadata *>{MDHelper.createString("dx.controlflow.hints"),
+                           MDHelper.createConstant(Op1)});
 
-      MDBuilder MDHelper(M.getContext());
-      ConstantInt *Op1 =
-          mdconst::extract<ConstantInt>(HlslControlFlowMD->getOperand(1));
+  MDNode *MDNode = llvm::MDNode::get(M.getContext(), Vals);
----------------
hekota wrote:

I think you can avoid the SmallVector:

```suggestion
  llvm::Metadata *HintsStr = MDHelper.createString("dx.controlflow.hints");
  llvm::Metadata *HintsValue = MDHelper.createConstant(Op1);
  MDNode *MDNode = llvm::MDNode::get(M.getContext(), { HintsStr, HintsValue});
```

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


More information about the llvm-commits mailing list