-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dx12] Drop code for llvm passes which prepare for DXIL generation. (#…
…5998) 2 passes are added for DXIL generation. TaichiIntrinsicLower will translate taichi intrinsic like thread_idx into the form DirectX backend expected. TaichiRuntimeContextLower will translate the TaichiRuntimeContext parameter for kernel into Buffers/ConstantBuffers. TaichiRuntimeContextLower is empty now. It is added after inline so optimizations reduce the load/store on temp ptr. And it is easier to know a store is on the TaichiRuntimeContext. Related issue = #5276 Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a9f2905
commit e7bdbff
Showing
5 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
|
||
#include "dx12_llvm_passes.h" | ||
#include "llvm/Pass.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/IR/IntrinsicsDirectX.h" | ||
#include "llvm/Transforms/Utils/ModuleUtils.h" | ||
|
||
#include "taichi/program/compile_config.h" | ||
#include "taichi/runtime/llvm/llvm_context.h" | ||
|
||
using namespace llvm; | ||
using namespace taichi::lang::directx12; | ||
|
||
#define DEBUG_TYPE "dxil-taichi-intrinsic-lower" | ||
|
||
namespace { | ||
|
||
class TaichiIntrinsicLower : public ModulePass { | ||
public: | ||
bool runOnModule(Module &M) override { | ||
auto &Ctx = M.getContext(); | ||
// patch intrinsic | ||
auto patch_intrinsic = [&](std::string name, Intrinsic::ID intrin, | ||
bool ret = true, | ||
std::vector<llvm::Type *> types = {}, | ||
std::vector<llvm::Value *> extra_args = {}) { | ||
auto func = M.getFunction(name); | ||
if (!func) { | ||
return; | ||
} | ||
func->deleteBody(); | ||
auto bb = llvm::BasicBlock::Create(Ctx, "entry", func); | ||
IRBuilder<> builder(Ctx); | ||
builder.SetInsertPoint(bb); | ||
std::vector<llvm::Value *> args; | ||
for (auto &arg : func->args()) | ||
args.push_back(&arg); | ||
args.insert(args.end(), extra_args.begin(), extra_args.end()); | ||
if (ret) { | ||
builder.CreateRet(builder.CreateIntrinsic(intrin, types, args)); | ||
} else { | ||
builder.CreateIntrinsic(intrin, types, args); | ||
builder.CreateRetVoid(); | ||
} | ||
func->setLinkage(GlobalValue::LinkageTypes::InternalLinkage); | ||
taichi::lang::TaichiLLVMContext::mark_inline(func); | ||
}; | ||
|
||
llvm::IRBuilder<> B(Ctx); | ||
Value *i32Zero = B.getInt32(0); | ||
|
||
auto patch_intrinsic_to_const = [&](std::string name, Constant *C, | ||
Type *Ty) { | ||
auto func = M.getFunction(name); | ||
if (!func) { | ||
return; | ||
} | ||
func->deleteBody(); | ||
auto bb = llvm::BasicBlock::Create(Ctx, "entry", func); | ||
IRBuilder<> B(Ctx); | ||
B.SetInsertPoint(bb); | ||
Value *V = C; | ||
if (V->getType()->isPointerTy()) | ||
V = B.CreateLoad(Ty, C); | ||
B.CreateRet(V); | ||
func->setLinkage(GlobalValue::LinkageTypes::InternalLinkage); | ||
taichi::lang::TaichiLLVMContext::mark_inline(func); | ||
}; | ||
// group thread id. | ||
patch_intrinsic("thread_idx", Intrinsic::dx_thread_id_in_group, true, {}, | ||
{i32Zero}); | ||
// group idx. | ||
patch_intrinsic("block_idx", Intrinsic::dx_group_id, true, {}, {i32Zero}); | ||
// Group Size | ||
unsigned group_size = 64; | ||
if (config) | ||
group_size = config->default_gpu_block_dim; | ||
|
||
auto *I32Ty = B.getInt32Ty(); | ||
Constant *block_dim = B.getInt32(group_size); | ||
patch_intrinsic_to_const("block_dim", block_dim, I32Ty); | ||
// Num work groups will be in a special CBuffer. | ||
// TaichiRuntimeContextLower pass will place the CBuffer to special binding | ||
// space. | ||
Type *TyNumWorkGroups = FixedVectorType::get(I32Ty, 3); | ||
Constant *CBNumWorkGroups = createGlobalVariableForResource( | ||
M, NumWorkGroupsCBName, TyNumWorkGroups); | ||
|
||
Constant *NumWorkGroupX = cast<Constant>( | ||
B.CreateConstGEP2_32(TyNumWorkGroups, CBNumWorkGroups, 0, 0)); | ||
patch_intrinsic_to_const("grid_dim", NumWorkGroupX, I32Ty); | ||
return true; | ||
} | ||
|
||
TaichiIntrinsicLower(taichi::lang::CompileConfig *config = nullptr) | ||
: ModulePass(ID), config(config) { | ||
initializeTaichiIntrinsicLowerPass(*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
static char ID; // Pass identification. | ||
private: | ||
taichi::lang::CompileConfig *config; | ||
}; | ||
char TaichiIntrinsicLower::ID = 0; | ||
|
||
} // end anonymous namespace | ||
|
||
INITIALIZE_PASS(TaichiIntrinsicLower, | ||
DEBUG_TYPE, | ||
"Lower taichi intrinsic", | ||
false, | ||
false) | ||
|
||
llvm::ModulePass *llvm::createTaichiIntrinsicLowerPass( | ||
taichi::lang::CompileConfig *config) { | ||
return new TaichiIntrinsicLower(config); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
|
||
#include "dx12_llvm_passes.h" | ||
|
||
#include "llvm/Pass.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/IR/Instructions.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/Transforms/Utils/ModuleUtils.h" | ||
|
||
#include "taichi/program/compile_config.h" | ||
#include "taichi/runtime/llvm/llvm_context.h" | ||
|
||
using namespace llvm; | ||
using namespace taichi::lang::directx12; | ||
|
||
#define DEBUG_TYPE "dxil-taichi-runtime-context-lower" | ||
|
||
namespace { | ||
|
||
class TaichiRuntimeContextLower : public ModulePass { | ||
public: | ||
bool runOnModule(Module &M) override { | ||
// TODO: lower taichi RuntimeContext into DXIL resources. | ||
return true; | ||
} | ||
|
||
TaichiRuntimeContextLower() : ModulePass(ID) { | ||
initializeTaichiRuntimeContextLowerPass(*PassRegistry::getPassRegistry()); | ||
} | ||
|
||
static char ID; // Pass identification. | ||
private: | ||
}; | ||
char TaichiRuntimeContextLower::ID = 0; | ||
|
||
} // end anonymous namespace | ||
|
||
INITIALIZE_PASS(TaichiRuntimeContextLower, | ||
DEBUG_TYPE, | ||
"Lower taichi RuntimeContext", | ||
false, | ||
false) | ||
|
||
llvm::ModulePass *llvm::createTaichiRuntimeContextLowerPass() { | ||
return new TaichiRuntimeContextLower(); | ||
} |