1 //===- ROCDLDialect.cpp - ROCDL IR Ops and Dialect registration -----------===//
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 // This file defines the types and operation details for the ROCDL IR dialect in
10 // MLIR, and the LLVM IR dialect.  It also registers the dialect.
11 //
12 // The ROCDL dialect only contains GPU specific additions on top of the general
13 // LLVM dialect.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
18 
19 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
20 #include "mlir/IR/Builders.h"
21 #include "mlir/IR/BuiltinTypes.h"
22 #include "mlir/IR/MLIRContext.h"
23 #include "mlir/IR/Operation.h"
24 #include "llvm/AsmParser/Parser.h"
25 #include "llvm/IR/Attributes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Support/SourceMgr.h"
29 
30 using namespace mlir;
31 using namespace ROCDL;
32 
33 #include "mlir/Dialect/LLVMIR/ROCDLOpsDialect.cpp.inc"
34 
35 //===----------------------------------------------------------------------===//
36 // Parsing for ROCDL ops
37 //===----------------------------------------------------------------------===//
38 
39 // <operation> ::=
40 //     `llvm.amdgcn.buffer.load.* %rsrc, %vindex, %offset, %glc, %slc :
41 //     result_type`
42 ParseResult MubufLoadOp::parse(OpAsmParser &parser, OperationState &result) {
43   SmallVector<OpAsmParser::OperandType, 8> ops;
44   Type type;
45   if (parser.parseOperandList(ops, 5) || parser.parseColonType(type) ||
46       parser.addTypeToList(type, result.types))
47     return failure();
48 
49   MLIRContext *context = parser.getContext();
50   auto int32Ty = IntegerType::get(context, 32);
51   auto int1Ty = IntegerType::get(context, 1);
52   auto i32x4Ty = LLVM::getFixedVectorType(int32Ty, 4);
53   return parser.resolveOperands(ops,
54                                 {i32x4Ty, int32Ty, int32Ty, int1Ty, int1Ty},
55                                 parser.getNameLoc(), result.operands);
56 }
57 
58 void MubufLoadOp::print(OpAsmPrinter &p) {
59   p << " " << getOperands() << " : " << (*this)->getResultTypes();
60 }
61 
62 // <operation> ::=
63 //     `llvm.amdgcn.buffer.store.* %vdata, %rsrc, %vindex, %offset, %glc, %slc :
64 //     result_type`
65 ParseResult MubufStoreOp::parse(OpAsmParser &parser, OperationState &result) {
66   SmallVector<OpAsmParser::OperandType, 8> ops;
67   Type type;
68   if (parser.parseOperandList(ops, 6) || parser.parseColonType(type))
69     return failure();
70 
71   MLIRContext *context = parser.getContext();
72   auto int32Ty = IntegerType::get(context, 32);
73   auto int1Ty = IntegerType::get(context, 1);
74   auto i32x4Ty = LLVM::getFixedVectorType(int32Ty, 4);
75 
76   if (parser.resolveOperands(ops,
77                              {type, i32x4Ty, int32Ty, int32Ty, int1Ty, int1Ty},
78                              parser.getNameLoc(), result.operands))
79     return failure();
80   return success();
81 }
82 
83 void MubufStoreOp::print(OpAsmPrinter &p) {
84   p << " " << getOperands() << " : " << vdata().getType();
85 }
86 
87 //===----------------------------------------------------------------------===//
88 // ROCDLDialect initialization, type parsing, and registration.
89 //===----------------------------------------------------------------------===//
90 
91 // TODO: This should be the llvm.rocdl dialect once this is supported.
92 void ROCDLDialect::initialize() {
93   addOperations<
94 #define GET_OP_LIST
95 #include "mlir/Dialect/LLVMIR/ROCDLOps.cpp.inc"
96       >();
97 
98   // Support unknown operations because not all ROCDL operations are registered.
99   allowUnknownOperations();
100 }
101 
102 LogicalResult ROCDLDialect::verifyOperationAttribute(Operation *op,
103                                                      NamedAttribute attr) {
104   // Kernel function attribute should be attached to functions.
105   if (attr.getName() == ROCDLDialect::getKernelFuncAttrName()) {
106     if (!isa<LLVM::LLVMFuncOp>(op)) {
107       return op->emitError() << "'" << ROCDLDialect::getKernelFuncAttrName()
108                              << "' attribute attached to unexpected op";
109     }
110   }
111   return success();
112 }
113 
114 #define GET_OP_CLASSES
115 #include "mlir/Dialect/LLVMIR/ROCDLOps.cpp.inc"
116