21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/IntrinsicsNVPTX.h"
29 NVVM::ReduxKind kind) {
30 if (!resultType->isIntegerTy(32))
31 llvm_unreachable(
"unsupported data type for redux");
34 case NVVM::ReduxKind::ADD:
35 return llvm::Intrinsic::nvvm_redux_sync_add;
36 case NVVM::ReduxKind::UMAX:
37 return llvm::Intrinsic::nvvm_redux_sync_umax;
38 case NVVM::ReduxKind::UMIN:
39 return llvm::Intrinsic::nvvm_redux_sync_umin;
40 case NVVM::ReduxKind::AND:
41 return llvm::Intrinsic::nvvm_redux_sync_and;
42 case NVVM::ReduxKind::OR:
43 return llvm::Intrinsic::nvvm_redux_sync_or;
44 case NVVM::ReduxKind::XOR:
45 return llvm::Intrinsic::nvvm_redux_sync_xor;
46 case NVVM::ReduxKind::MAX:
47 return llvm::Intrinsic::nvvm_redux_sync_max;
48 case NVVM::ReduxKind::MIN:
49 return llvm::Intrinsic::nvvm_redux_sync_min;
51 llvm_unreachable(
"unknown redux kind");
59 resultType = cast<llvm::StructType>(resultType)->getElementType(0);
61 case NVVM::ShflKind::bfly:
62 return resultType->isFloatTy()
63 ? llvm::Intrinsic::nvvm_shfl_sync_bfly_f32p
64 : llvm::Intrinsic::nvvm_shfl_sync_bfly_i32p;
65 case NVVM::ShflKind::up:
66 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_up_f32p
67 : llvm::Intrinsic::nvvm_shfl_sync_up_i32p;
68 case NVVM::ShflKind::down:
69 return resultType->isFloatTy()
70 ? llvm::Intrinsic::nvvm_shfl_sync_down_f32p
71 : llvm::Intrinsic::nvvm_shfl_sync_down_i32p;
72 case NVVM::ShflKind::idx:
73 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_idx_f32p
74 : llvm::Intrinsic::nvvm_shfl_sync_idx_i32p;
78 case NVVM::ShflKind::bfly:
79 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_bfly_f32
80 : llvm::Intrinsic::nvvm_shfl_sync_bfly_i32;
81 case NVVM::ShflKind::up:
82 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_up_f32
83 : llvm::Intrinsic::nvvm_shfl_sync_up_i32;
84 case NVVM::ShflKind::down:
85 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_down_f32
86 : llvm::Intrinsic::nvvm_shfl_sync_down_i32;
87 case NVVM::ShflKind::idx:
88 return resultType->isFloatTy() ? llvm::Intrinsic::nvvm_shfl_sync_idx_f32
89 : llvm::Intrinsic::nvvm_shfl_sync_idx_i32;
92 llvm_unreachable(
"unknown shuffle kind");
98 if (layout == NVVM::MMALayout::row) {
101 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x1_b16;
103 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x2_b16;
105 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x4_b16;
107 llvm_unreachable(
"unsupported number of matrix");
113 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x1_trans_b16;
115 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x2_trans_b16;
117 return llvm::Intrinsic::nvvm_ldmatrix_sync_aligned_m8n8_x4_trans_b16;
119 llvm_unreachable(
"unsupported number of matrix");
127 class NVVMDialectLLVMIRTranslationInterface
135 convertOperation(
Operation *op, llvm::IRBuilderBase &builder,
138 #include "mlir/Dialect/LLVMIR/NVVMConversions.inc"
147 auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
150 llvm::LLVMContext &llvmContext = moduleTranslation.getLLVMContext();
151 llvm::Function *llvmFunc = moduleTranslation.lookupFunction(func.getName());
153 auto generateMetadata = [&](
int dim, StringRef name) {
154 llvm::Metadata *llvmMetadata[] = {
158 llvm::Type::getInt32Ty(llvmContext), dim))};
159 llvm::MDNode *llvmMetadataNode =
161 moduleTranslation.getOrInsertNamedModuleMetadata(
"nvvm.annotations")
162 ->addOperand(llvmMetadataNode);
164 if (attribute.getName() == NVVM::NVVMDialect::getMaxntidAttrName()) {
165 if (!dyn_cast<ArrayAttr>(attribute.getValue()))
169 generateMetadata(values[0], NVVM::NVVMDialect::getMaxntidXName());
170 if (values.size() > 1)
171 generateMetadata(values[1], NVVM::NVVMDialect::getMaxntidYName());
172 if (values.size() > 2)
173 generateMetadata(values[2], NVVM::NVVMDialect::getMaxntidZName());
174 }
else if (attribute.getName() == NVVM::NVVMDialect::getReqntidAttrName()) {
175 if (!dyn_cast<ArrayAttr>(attribute.getValue()))
179 generateMetadata(values[0], NVVM::NVVMDialect::getReqntidXName());
180 if (values.size() > 1)
181 generateMetadata(values[1], NVVM::NVVMDialect::getReqntidYName());
182 if (values.size() > 2)
183 generateMetadata(values[2], NVVM::NVVMDialect::getReqntidZName());
184 }
else if (attribute.getName() ==
185 NVVM::NVVMDialect::getMinctasmAttrName()) {
186 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
187 generateMetadata(value.getInt(),
"minctasm");
188 }
else if (attribute.getName() == NVVM::NVVMDialect::getMaxnregAttrName()) {
189 auto value = dyn_cast<IntegerAttr>(attribute.getValue());
190 generateMetadata(value.getInt(),
"maxnreg");
191 }
else if (attribute.getName() ==
192 NVVM::NVVMDialect::getKernelFuncAttrName()) {
193 llvm::Metadata *llvmMetadataKernel[] = {
198 llvm::MDNode *llvmMetadataNode =
200 moduleTranslation.getOrInsertNamedModuleMetadata(
"nvvm.annotations")
201 ->addOperand(llvmMetadataNode);
209 registry.
insert<NVVM::NVVMDialect>();
211 dialect->addInterfaces<NVVMDialectLLVMIRTranslationInterface>();
static llvm::Intrinsic::ID getReduxIntrinsicId(llvm::Type *resultType, NVVM::ReduxKind kind)
static llvm::Intrinsic::ID getShflIntrinsicId(llvm::Type *resultType, NVVM::ShflKind kind, bool withPredicate)
static llvm::Intrinsic::ID getLdMatrixIntrinsicId(NVVM::MMALayout layout, int32_t num)
Return the intrinsic ID associated with ldmatrix for the given paramters.
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
void addExtension(std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Base class for dialect interfaces providing translation to LLVM IR.
LLVMTranslationDialectInterface(Dialect *dialect)
Implementation class for module translation.
MLIRContext is the top-level object for a collection of MLIR operations.
void appendDialectRegistry(const DialectRegistry ®istry)
Append the contents of the given dialect registry to the registry associated with this context.
NamedAttribute represents a combination of a name and an Attribute value.
Operation is the basic unit of execution within MLIR.
llvm::Value * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value * > args={}, ArrayRef< llvm::Type * > tys={})
Creates a call to an LLVM IR intrinsic function with the given arguments.
This header declares functions that assist transformations in the MemRef dialect.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
void registerNVVMDialectTranslation(DialectRegistry ®istry)
Register the NVVM dialect and the translation from it to the LLVM IR in the given registry;.
SmallVector< int64_t, 4 > extractFromI64ArrayAttr(Attribute attr)
Extract int64_t values from the assumed ArrayAttr of IntegerAttr.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
This class represents an efficient way to signal success or failure.