LLVM 18.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"
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
29using namespace llvm;
30using namespace llvm::dxil;
31
32static 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
56static 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
78namespace {
79/// A pass that transforms external global definitions into declarations.
80class DXILOpLowering : public PassInfoMixin<DXILOpLowering> {
81public:
83 if (lowerIntrinsics(M))
86 }
87};
88} // namespace
89
90namespace {
91class DXILOpLoweringLegacy : public ModulePass {
92public:
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};
99char DXILOpLoweringLegacy::ID = 0;
100
101} // end anonymous namespace
102
103INITIALIZE_PASS_BEGIN(DXILOpLoweringLegacy, DEBUG_TYPE, "DXIL Op Lowering",
104 false, false)
105INITIALIZE_PASS_END(DXILOpLoweringLegacy, DEBUG_TYPE, "DXIL Op Lowering", false,
106 false)
107
109 return new DXILOpLoweringLegacy();
110}
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
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
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:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
This file defines the SmallVector class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:649
User::op_iterator arg_begin()
Return the iterator pointing to the beginning of the argument list.
Definition: InstrTypes.h:1369
User::op_iterator arg_end()
Return the iterator pointing to the end of the argument list.
Definition: InstrTypes.h:1375
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1385
This class represents a function call, abstracting a target machine's calling convention.
This class represents an Operation in the Expression.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2636
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:93
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 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: PassManager.h:172
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:175
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:178
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
Type * getOverloadTy(dxil::OpCode OpCode, FunctionType *FT, bool NoOpCodeParam)
CallInst * createDXILOpCall(dxil::OpCode OpCode, Type *OverloadTy, llvm::iterator_range< Use * > Args)
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:665
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:391