LLVM  16.0.0git
DXILOpLowering.cpp
Go to the documentation of this file.
1 //===- DXILOpLower.cpp - Lowering LLVM intrinsic to DIXLOp function -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file This file contains passes and utilities to lower llvm intrinsic call
10 /// to DXILOp function call.
11 //===----------------------------------------------------------------------===//
12 
13 #include "DXILConstants.h"
14 #include "DXILOpBuilder.h"
15 #include "DirectX.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instruction.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/IR/IntrinsicsDirectX.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/Pass.h"
26 
27 #define DEBUG_TYPE "dxil-op-lower"
28 
29 using namespace llvm;
30 using namespace llvm::DXIL;
31 
32 static void lowerIntrinsic(DXIL::OpCode DXILOp, Function &F, Module &M) {
33  IRBuilder<> B(M.getContext());
34  Value *DXILOpArg = B.getInt32(static_cast<unsigned>(DXILOp));
35  DXILOpBuilder DXILB(M, B);
36  Type *OverloadTy =
37  DXILB.getOverloadTy(DXILOp, F.getFunctionType(), /*NoOpCodeParam*/ true);
38  for (User *U : make_early_inc_range(F.users())) {
39  CallInst *CI = dyn_cast<CallInst>(U);
40  if (!CI)
41  continue;
42 
44  Args.emplace_back(DXILOpArg);
45  Args.append(CI->arg_begin(), CI->arg_end());
46  B.SetInsertPoint(CI);
47  CallInst *DXILCI = DXILB.createDXILOpCall(DXILOp, OverloadTy, CI->args());
48 
49  CI->replaceAllUsesWith(DXILCI);
50  CI->eraseFromParent();
51  }
52  if (F.user_empty())
53  F.eraseFromParent();
54 }
55 
56 static bool lowerIntrinsics(Module &M) {
57  bool Updated = false;
58 
59 #define DXIL_OP_INTRINSIC_MAP
60 #include "DXILOperation.inc"
61 #undef DXIL_OP_INTRINSIC_MAP
62 
63  for (Function &F : make_early_inc_range(M.functions())) {
64  if (!F.isDeclaration())
65  continue;
66  Intrinsic::ID ID = F.getIntrinsicID();
68  continue;
69  auto LowerIt = LowerMap.find(ID);
70  if (LowerIt == LowerMap.end())
71  continue;
72  lowerIntrinsic(LowerIt->second, F, M);
73  Updated = true;
74  }
75  return Updated;
76 }
77 
78 namespace {
79 /// A pass that transforms external global definitions into declarations.
80 class DXILOpLowering : public PassInfoMixin<DXILOpLowering> {
81 public:
83  if (lowerIntrinsics(M))
84  return PreservedAnalyses::none();
85  return PreservedAnalyses::all();
86  }
87 };
88 } // namespace
89 
90 namespace {
91 class DXILOpLoweringLegacy : public ModulePass {
92 public:
93  bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
94  StringRef getPassName() const override { return "DXIL Op Lowering"; }
95  DXILOpLoweringLegacy() : ModulePass(ID) {}
96 
97  static char ID; // Pass identification.
98 };
100 
101 } // end anonymous namespace
102 
103 INITIALIZE_PASS_BEGIN(DXILOpLoweringLegacy, DEBUG_TYPE, "DXIL Op Lowering",
104  false, false)
105 INITIALIZE_PASS_END(DXILOpLoweringLegacy, DEBUG_TYPE, "DXIL Op Lowering", false,
106  false)
107 
109  return new DXILOpLoweringLegacy();
110 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:371
llvm::Function
Definition: Function.h:60
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1182
ErrorHandling.h
llvm::IRBuilder<>
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::Intrinsic::not_intrinsic
@ not_intrinsic
Definition: Intrinsics.h:45
llvm::CallBase::arg_begin
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1316
llvm::DXIL::DXILOpBuilder
Definition: DXILOpBuilder.h:29
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::DXIL::DXILOpBuilder::getOverloadTy
Type * getOverloadTy(DXIL::OpCode OpCode, FunctionType *FT, bool NoOpCodeParam)
Definition: DXILOpBuilder.cpp:269
Instruction.h
DirectX.h
llvm::User
Definition: User.h:44
Intrinsics.h
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(DXILOpLoweringLegacy, DEBUG_TYPE, "DXIL Op Lowering", false, false) INITIALIZE_PASS_END(DXILOpLoweringLegacy
llvm::createDXILOpLoweringLegacyPass
ModulePass * createDXILOpLoweringLegacyPass()
Pass to lowering LLVM intrinsic call to DXIL op function call.
Definition: DXILOpLowering.cpp:108
lowerIntrinsics
static bool lowerIntrinsics(Module &M)
Definition: DXILOpLowering.cpp:56
false
Definition: StackSlotColoring.cpp:141
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::dxil::PointerTypeAnalysis::run
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
Definition: PointerTypeAnalysis.cpp:101
Passes.h
DXILOpBuilder.h
llvm::Instruction::eraseFromParent
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:77
llvm::DXIL
Definition: DXILOperationCommon.h:23
Lowering
DXIL Op Lowering
Definition: DXILOpLowering.cpp:105
llvm::make_early_inc_range
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:596
DXILConstants.h
IRBuilder.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::CallBase::arg_end
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1322
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:532
llvm::DXIL::DXILOpBuilder::createDXILOpCall
CallInst * createDXILOpCall(DXIL::OpCode OpCode, Type *OverloadTy, llvm::iterator_range< Use * > Args)
Definition: DXILOpBuilder.cpp:260
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:348
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
PassManager.h
DEBUG_TYPE
#define DEBUG_TYPE
Definition: DXILOpLowering.cpp:27
SmallVector.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition: Instructions.h:1474
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:394
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
lowerIntrinsic
static void lowerIntrinsic(DXIL::OpCode DXILOp, Function &F, Module &M)
Definition: DXILOpLowering.cpp:32
llvm::CallBase::args
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1332
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38