|
15 | 15 | #include "llvm/ADT/Twine.h" |
16 | 16 | #include "llvm/Analysis/DXILMetadataAnalysis.h" |
17 | 17 | #include "llvm/Analysis/DXILResource.h" |
| 18 | +#include "llvm/IR/BasicBlock.h" |
18 | 19 | #include "llvm/IR/Constants.h" |
19 | 20 | #include "llvm/IR/DiagnosticInfo.h" |
20 | 21 | #include "llvm/IR/DiagnosticPrinter.h" |
21 | 22 | #include "llvm/IR/Function.h" |
22 | 23 | #include "llvm/IR/IRBuilder.h" |
23 | 24 | #include "llvm/IR/LLVMContext.h" |
| 25 | +#include "llvm/IR/MDBuilder.h" |
24 | 26 | #include "llvm/IR/Metadata.h" |
25 | 27 | #include "llvm/IR/Module.h" |
26 | 28 | #include "llvm/InitializePasses.h" |
@@ -300,6 +302,39 @@ static MDTuple *emitTopLevelLibraryNode(Module &M, MDNode *RMD, |
300 | 302 | return constructEntryMetadata(nullptr, nullptr, RMD, Properties, Ctx); |
301 | 303 | } |
302 | 304 |
|
| 305 | +// TODO: We might need to refactor this to be more generic, |
| 306 | +// in case we need more metadata to be replaced. |
| 307 | +static void translateBranchMetadata(Module &M) { |
| 308 | + for (Function &F : M) { |
| 309 | + for (BasicBlock &BB : F) { |
| 310 | + Instruction *BBTerminatorInst = BB.getTerminator(); |
| 311 | + |
| 312 | + MDNode *HlslControlFlowMD = |
| 313 | + BBTerminatorInst->getMetadata("hlsl.controlflow.hint"); |
| 314 | + |
| 315 | + if (!HlslControlFlowMD) |
| 316 | + continue; |
| 317 | + |
| 318 | + assert(HlslControlFlowMD->getNumOperands() == 2 && |
| 319 | + "invalid operands for hlsl.controlflow.hint"); |
| 320 | + |
| 321 | + MDBuilder MDHelper(M.getContext()); |
| 322 | + ConstantInt *Op1 = |
| 323 | + mdconst::extract<ConstantInt>(HlslControlFlowMD->getOperand(1)); |
| 324 | + |
| 325 | + SmallVector<llvm::Metadata *, 2> Vals( |
| 326 | + ArrayRef<Metadata *>{MDHelper.createString("dx.controlflow.hints"), |
| 327 | + MDHelper.createConstant(Op1)}); |
| 328 | + |
| 329 | + MDNode *MDNode = llvm::MDNode::get(M.getContext(), Vals); |
| 330 | + |
| 331 | + BBTerminatorInst->setMetadata("dx.controlflow.hints", MDNode); |
| 332 | + BBTerminatorInst->setMetadata("hlsl.controlflow.hint", nullptr); |
| 333 | + } |
| 334 | + F.clearMetadata(); |
| 335 | + } |
| 336 | +} |
| 337 | + |
303 | 338 | static void translateMetadata(Module &M, DXILBindingMap &DBM, |
304 | 339 | DXILResourceTypeMap &DRTM, |
305 | 340 | const Resources &MDResources, |
@@ -372,6 +407,7 @@ PreservedAnalyses DXILTranslateMetadata::run(Module &M, |
372 | 407 | const dxil::ModuleMetadataInfo MMDI = MAM.getResult<DXILMetadataAnalysis>(M); |
373 | 408 |
|
374 | 409 | translateMetadata(M, DBM, DRTM, MDResources, ShaderFlags, MMDI); |
| 410 | + translateBranchMetadata(M); |
375 | 411 |
|
376 | 412 | return PreservedAnalyses::all(); |
377 | 413 | } |
@@ -409,6 +445,7 @@ class DXILTranslateMetadataLegacy : public ModulePass { |
409 | 445 | getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata(); |
410 | 446 |
|
411 | 447 | translateMetadata(M, DBM, DRTM, MDResources, ShaderFlags, MMDI); |
| 448 | + translateBranchMetadata(M); |
412 | 449 | return true; |
413 | 450 | } |
414 | 451 | }; |
|
0 commit comments