LLVM 19.0.0git
DXILPrepare.cpp
Go to the documentation of this file.
1//===- DXILPrepare.cpp - Prepare LLVM Module for DXIL encoding ------------===//
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 pases and utilities to convert a modern LLVM
10/// module into a module compatible with the LLVM 3.7-based DirectX Intermediate
11/// Language (DXIL).
12//===----------------------------------------------------------------------===//
13
14#include "DirectX.h"
16#include "llvm/ADT/STLExtras.h"
18#include "llvm/CodeGen/Passes.h"
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/Instruction.h"
22#include "llvm/IR/Module.h"
24#include "llvm/Pass.h"
26
27#define DEBUG_TYPE "dxil-prepare"
28
29using namespace llvm;
30using namespace llvm::dxil;
31
32namespace {
33
34constexpr bool isValidForDXIL(Attribute::AttrKind Attr) {
35 return is_contained({Attribute::Alignment,
36 Attribute::AlwaysInline,
37 Attribute::Builtin,
38 Attribute::ByVal,
39 Attribute::InAlloca,
40 Attribute::Cold,
41 Attribute::Convergent,
42 Attribute::InlineHint,
43 Attribute::InReg,
44 Attribute::JumpTable,
45 Attribute::MinSize,
46 Attribute::Naked,
47 Attribute::Nest,
48 Attribute::NoAlias,
49 Attribute::NoBuiltin,
50 Attribute::NoCapture,
51 Attribute::NoDuplicate,
52 Attribute::NoImplicitFloat,
53 Attribute::NoInline,
54 Attribute::NonLazyBind,
55 Attribute::NonNull,
56 Attribute::Dereferenceable,
57 Attribute::DereferenceableOrNull,
58 Attribute::Memory,
59 Attribute::NoRedZone,
60 Attribute::NoReturn,
61 Attribute::NoUnwind,
62 Attribute::OptimizeForSize,
63 Attribute::OptimizeNone,
64 Attribute::ReadNone,
65 Attribute::ReadOnly,
66 Attribute::Returned,
67 Attribute::ReturnsTwice,
68 Attribute::SExt,
69 Attribute::StackAlignment,
70 Attribute::StackProtect,
71 Attribute::StackProtectReq,
72 Attribute::StackProtectStrong,
73 Attribute::SafeStack,
74 Attribute::StructRet,
75 Attribute::SanitizeAddress,
76 Attribute::SanitizeThread,
77 Attribute::SanitizeMemory,
78 Attribute::UWTable,
79 Attribute::ZExt},
80 Attr);
81}
82
83class DXILPrepareModule : public ModulePass {
84
85 static Value *maybeGenerateBitcast(IRBuilder<> &Builder,
86 PointerTypeMap &PointerTypes,
87 Instruction &Inst, Value *Operand,
88 Type *Ty) {
89 // Omit bitcasts if the incoming value matches the instruction type.
90 auto It = PointerTypes.find(Operand);
91 if (It != PointerTypes.end())
92 if (cast<TypedPointerType>(It->second)->getElementType() == Ty)
93 return nullptr;
94 // Insert bitcasts where we are removing the instruction.
95 Builder.SetInsertPoint(&Inst);
96 // This code only gets hit in opaque-pointer mode, so the type of the
97 // pointer doesn't matter.
98 PointerType *PtrTy = cast<PointerType>(Operand->getType());
99 return Builder.Insert(
100 CastInst::Create(Instruction::BitCast, Operand,
101 Builder.getPtrTy(PtrTy->getAddressSpace())));
102 }
103
104public:
105 bool runOnModule(Module &M) override {
107 AttributeMask AttrMask;
109 I = Attribute::AttrKind(I + 1)) {
110 if (!isValidForDXIL(I))
111 AttrMask.addAttribute(I);
112 }
113 for (auto &F : M.functions()) {
114 F.removeFnAttrs(AttrMask);
115 F.removeRetAttrs(AttrMask);
116 for (size_t Idx = 0, End = F.arg_size(); Idx < End; ++Idx)
117 F.removeParamAttrs(Idx, AttrMask);
118
119 for (auto &BB : F) {
120 IRBuilder<> Builder(&BB);
121 for (auto &I : make_early_inc_range(BB)) {
122 if (I.getOpcode() == Instruction::FNeg) {
123 Builder.SetInsertPoint(&I);
124 Value *In = I.getOperand(0);
125 Value *Zero = ConstantFP::get(In->getType(), -0.0);
126 I.replaceAllUsesWith(Builder.CreateFSub(Zero, In));
127 I.eraseFromParent();
128 continue;
129 }
130
131 // Emtting NoOp bitcast instructions allows the ValueEnumerator to be
132 // unmodified as it reserves instruction IDs during contruction.
133 if (auto LI = dyn_cast<LoadInst>(&I)) {
134 if (Value *NoOpBitcast = maybeGenerateBitcast(
135 Builder, PointerTypes, I, LI->getPointerOperand(),
136 LI->getType())) {
137 LI->replaceAllUsesWith(
138 Builder.CreateLoad(LI->getType(), NoOpBitcast));
139 LI->eraseFromParent();
140 }
141 continue;
142 }
143 if (auto SI = dyn_cast<StoreInst>(&I)) {
144 if (Value *NoOpBitcast = maybeGenerateBitcast(
145 Builder, PointerTypes, I, SI->getPointerOperand(),
146 SI->getValueOperand()->getType())) {
147
148 SI->replaceAllUsesWith(
149 Builder.CreateStore(SI->getValueOperand(), NoOpBitcast));
150 SI->eraseFromParent();
151 }
152 continue;
153 }
154 if (auto GEP = dyn_cast<GetElementPtrInst>(&I)) {
155 if (Value *NoOpBitcast = maybeGenerateBitcast(
156 Builder, PointerTypes, I, GEP->getPointerOperand(),
157 GEP->getSourceElementType()))
158 GEP->setOperand(0, NoOpBitcast);
159 continue;
160 }
161 if (auto *CB = dyn_cast<CallBase>(&I)) {
162 CB->removeFnAttrs(AttrMask);
163 CB->removeRetAttrs(AttrMask);
164 for (size_t Idx = 0, End = CB->arg_size(); Idx < End; ++Idx)
165 CB->removeParamAttrs(Idx, AttrMask);
166 continue;
167 }
168 }
169 }
170 }
171 return true;
172 }
173
174 DXILPrepareModule() : ModulePass(ID) {}
175
176 static char ID; // Pass identification.
177};
178char DXILPrepareModule::ID = 0;
179
180} // end anonymous namespace
181
182INITIALIZE_PASS_BEGIN(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module",
183 false, false)
184INITIALIZE_PASS_END(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module", false,
185 false)
186
188 return new DXILPrepareModule();
189}
#define DEBUG_TYPE
Definition: DXILPrepare.cpp:27
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
bool End
Definition: ELF_riscv.cpp:480
Hexagon Common GEP
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Module.h This file contains the declarations for the Module class.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
AttributeMask & addAttribute(Attribute::AttrKind Val)
Add an attribute to the mask.
Definition: AttributeMask.h:44
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:85
@ None
No attributes have been set.
Definition: Attributes.h:87
@ EndAttrKinds
Sentinel value useful for loops.
Definition: Attributes.h:90
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
iterator end()
Definition: DenseMap.h:84
Value * CreateFSub(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1560
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
Definition: IRBuilder.h:145
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1790
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1803
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:569
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:656
ModulePass * createDXILPrepareModulePass()
Pass to convert modules into DXIL-compatable modules.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879