17 #define DEBUG_TYPE "ptx-builder"
18 #define DBGS() (llvm::dbgs() << "[" DEBUG_TYPE "]: ")
19 #define DBGSNL() (llvm::dbgs() << "\n")
25 #include "mlir/Dialect/LLVMIR/BasicPtxBuilderInterface.cpp.inc"
45 if (
auto ptr = dyn_cast<LLVM::LLVMPointerType>(type)) {
53 llvm_unreachable(
"The register type could not deduced from MLIR type");
64 LLVM_DEBUG(
DBGS() << v <<
"\t Modifier : " << &itype <<
"\n");
65 auto getModifier = [&]() ->
const char * {
67 assert(
false &&
"Read-Write modifier is not supported. Try setting the "
68 "same value as Write and Read seperately.");
76 auto addValue = [&](
Value v) {
78 ptxOperands.push_back(v);
82 ptxOperands.push_back(v);
86 llvm::raw_string_ostream ss(registerConstraints);
88 if (
auto stype = dyn_cast<LLVM::LLVMStructType>(v.
getType())) {
94 Value extractValue = rewriter.
create<LLVM::ExtractValueOp>(
95 interfaceOp->getLoc(), v, idx);
96 addValue(extractValue);
115 LLVM::AsmDialect::AD_ATT);
117 auto resultTypes = interfaceOp->getResultTypes();
120 if (!registerConstraints.empty() &&
121 registerConstraints[registerConstraints.size() - 1] ==
',')
122 registerConstraints.pop_back();
124 std::string ptxInstruction = interfaceOp.getPtx();
127 if (interfaceOp.getPredicate().has_value() &&
128 interfaceOp.getPredicate().value()) {
129 std::string predicateStr =
"@%";
130 predicateStr += std::to_string((ptxOperands.size() - 1));
131 ptxInstruction = predicateStr +
" " + ptxInstruction;
136 std::replace(ptxInstruction.begin(), ptxInstruction.end(),
'%',
'$');
138 return rewriter.
create<LLVM::InlineAsmOp>(
139 interfaceOp->getLoc(),
142 llvm::StringRef(ptxInstruction),
143 registerConstraints.data(),
144 interfaceOp.hasSideEffect(),
151 LLVM::InlineAsmOp inlineAsmOp =
build();
152 LLVM_DEBUG(
DBGS() <<
"\n Generated PTX \n\t" << inlineAsmOp <<
"\n");
153 if (inlineAsmOp->getNumResults() == interfaceOp->getNumResults()) {
154 rewriter.
replaceOp(interfaceOp, inlineAsmOp);
static char getRegisterType(Type type)
void insertValue(Value v, PTXRegisterMod itype=PTXRegisterMod::Read)
Add an operand with the read/write input type.
LLVM::InlineAsmOp build()
Builds the inline assembly Op and returns it.
void buildAndReplaceOp()
Shortcut to build the inline assembly Op and replace or erase the original op with.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
bool isInteger() const
Return true if this is an integer type (with the specified width).
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
PTXRegisterMod
Register read/write modifier to build constraint string for PTX inline https://docs....
@ Write
Read register with '+' modifier.
@ ReadWrite
Read register with '=' modifier.
@ Read
Read register with no modifier.
@ kSharedMemorySpace
Shared memory space identifier.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...