|
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,38 @@ 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 | + } |
| 335 | +} |
| 336 | + |
303 | 337 | static void translateMetadata(Module &M, DXILBindingMap &DBM,
|
304 | 338 | DXILResourceTypeMap &DRTM,
|
305 | 339 | const Resources &MDResources,
|
@@ -372,6 +406,7 @@ PreservedAnalyses DXILTranslateMetadata::run(Module &M,
|
372 | 406 | const dxil::ModuleMetadataInfo MMDI = MAM.getResult<DXILMetadataAnalysis>(M);
|
373 | 407 |
|
374 | 408 | translateMetadata(M, DBM, DRTM, MDResources, ShaderFlags, MMDI);
|
| 409 | + translateBranchMetadata(M); |
375 | 410 |
|
376 | 411 | return PreservedAnalyses::all();
|
377 | 412 | }
|
@@ -409,6 +444,7 @@ class DXILTranslateMetadataLegacy : public ModulePass {
|
409 | 444 | getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
|
410 | 445 |
|
411 | 446 | translateMetadata(M, DBM, DRTM, MDResources, ShaderFlags, MMDI);
|
| 447 | + translateBranchMetadata(M); |
412 | 448 | return true;
|
413 | 449 | }
|
414 | 450 | };
|
|
0 commit comments