LLVM 20.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"
15#include "DXILOpBuilder.h"
16#include "DirectX.h"
18#include "llvm/CodeGen/Passes.h"
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/Instruction.h"
22#include "llvm/IR/Intrinsics.h"
23#include "llvm/IR/IntrinsicsDirectX.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/PassManager.h"
26#include "llvm/Pass.h"
28
29#define DEBUG_TYPE "dxil-op-lower"
30
31using namespace llvm;
32using namespace llvm::dxil;
33
35 switch (F.getIntrinsicID()) {
36 case Intrinsic::dx_dot2:
37 case Intrinsic::dx_dot3:
38 case Intrinsic::dx_dot4:
39 return true;
40 }
41 return false;
42}
43
45 SmallVector<Value *> ExtractedElements;
46 auto *VecArg = dyn_cast<FixedVectorType>(Arg->getType());
47 for (unsigned I = 0; I < VecArg->getNumElements(); ++I) {
48 Value *Index = ConstantInt::get(Type::getInt32Ty(Arg->getContext()), I);
49 Value *ExtractedElement = Builder.CreateExtractElement(Arg, Index);
50 ExtractedElements.push_back(ExtractedElement);
51 }
52 return ExtractedElements;
53}
54
56 IRBuilder<> &Builder) {
57 // Note: arg[NumOperands-1] is a pointer and is not needed by our flattening.
58 unsigned NumOperands = Orig->getNumOperands() - 1;
59 assert(NumOperands > 0);
60 Value *Arg0 = Orig->getOperand(0);
61 [[maybe_unused]] auto *VecArg0 = dyn_cast<FixedVectorType>(Arg0->getType());
62 assert(VecArg0);
63 SmallVector<Value *> NewOperands = populateOperands(Arg0, Builder);
64 for (unsigned I = 1; I < NumOperands; ++I) {
65 Value *Arg = Orig->getOperand(I);
66 [[maybe_unused]] auto *VecArg = dyn_cast<FixedVectorType>(Arg->getType());
67 assert(VecArg);
68 assert(VecArg0->getElementType() == VecArg->getElementType());
69 assert(VecArg0->getNumElements() == VecArg->getNumElements());
70 auto NextOperandList = populateOperands(Arg, Builder);
71 NewOperands.append(NextOperandList.begin(), NextOperandList.end());
72 }
73 return NewOperands;
74}
75
76static void lowerIntrinsic(dxil::OpCode DXILOp, Function &F, Module &M) {
77 IRBuilder<> B(M.getContext());
78 DXILOpBuilder OpBuilder(M, B);
79 for (User *U : make_early_inc_range(F.users())) {
80 CallInst *CI = dyn_cast<CallInst>(U);
81 if (!CI)
82 continue;
83
85 B.SetInsertPoint(CI);
88 Args.append(NewArgs.begin(), NewArgs.end());
89 } else
90 Args.append(CI->arg_begin(), CI->arg_end());
91
92 Expected<CallInst *> OpCallOrErr = OpBuilder.tryCreateOp(DXILOp, Args,
93 F.getReturnType());
94 if (Error E = OpCallOrErr.takeError()) {
95 std::string Message(toString(std::move(E)));
96 DiagnosticInfoUnsupported Diag(*CI->getFunction(), Message,
97 CI->getDebugLoc());
98 M.getContext().diagnose(Diag);
99 continue;
100 }
101 CallInst *OpCall = *OpCallOrErr;
102
103 CI->replaceAllUsesWith(OpCall);
104 CI->eraseFromParent();
105 }
106 if (F.user_empty())
107 F.eraseFromParent();
108}
109
110static bool lowerIntrinsics(Module &M) {
111 bool Updated = false;
112
113 for (Function &F : make_early_inc_range(M.functions())) {
114 if (!F.isDeclaration())
115 continue;
116 Intrinsic::ID ID = F.getIntrinsicID();
117 switch (ID) {
118 default:
119 continue;
120#define DXIL_OP_INTRINSIC(OpCode, Intrin) \
121 case Intrin: \
122 lowerIntrinsic(OpCode, F, M); \
123 break;
124#include "DXILOperation.inc"
125 }
126 Updated = true;
127 }
128 return Updated;
129}
130
131namespace {
132/// A pass that transforms external global definitions into declarations.
133class DXILOpLowering : public PassInfoMixin<DXILOpLowering> {
134public:
136 if (lowerIntrinsics(M))
138 return PreservedAnalyses::all();
139 }
140};
141} // namespace
142
143namespace {
144class DXILOpLoweringLegacy : public ModulePass {
145public:
146 bool runOnModule(Module &M) override { return lowerIntrinsics(M); }
147 StringRef getPassName() const override { return "DXIL Op Lowering"; }
148 DXILOpLoweringLegacy() : ModulePass(ID) {}
149
150 static char ID; // Pass identification.
151 void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
152 // Specify the passes that your pass depends on
154 }
155};
156char DXILOpLoweringLegacy::ID = 0;
157} // end anonymous namespace
158
159INITIALIZE_PASS_BEGIN(DXILOpLoweringLegacy, DEBUG_TYPE, "DXIL Op Lowering",
160 false, false)
161INITIALIZE_PASS_END(DXILOpLoweringLegacy, DEBUG_TYPE, "DXIL Op Lowering", false,
162 false)
163
165 return new DXILOpLoweringLegacy();
166}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool isVectorArgExpansion(Function &F)
static SmallVector< Value * > argVectorFlatten(CallInst *Orig, IRBuilder<> &Builder)
static SmallVector< Value * > populateOperands(Value *Arg, IRBuilder<> &Builder)
static bool lowerIntrinsics(Module &M)
DXIL Op Lowering
#define DEBUG_TYPE
static void lowerIntrinsic(dxil::OpCode DXILOp, Function &F, Module &M)
#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.
This header defines various interfaces for pass management in LLVM.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1385
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1391
This class represents a function call, abstracting a target machine's calling convention.
This class represents an Operation in the Expression.
Diagnostic information for unsupported feature in backend.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2480
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:466
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
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
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
static IntegerType * getInt32Ty(LLVMContext &C)
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1075
Expected< CallInst * > tryCreateOp(dxil::OpCode Op, ArrayRef< Value * > Args, Type *RetTy=nullptr)
Try to create a call instruction for the given DXIL op.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
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 * createDXILOpLoweringLegacyPass()
Pass to lowering LLVM intrinsic call to DXIL op function call.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69