1 //===- ROCDLDialect.cpp - ROCDL IR Ops and Dialect registration -----------===//
2 //
3 // Copyright 2019 The MLIR Authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // =============================================================================
17 //
18 // This file defines the types and operation details for the ROCDL IR dialect in
19 // MLIR, and the LLVM IR dialect.  It also registers the dialect.
20 //
21 // The ROCDL dialect only contains GPU specific additions on top of the general
22 // LLVM dialect.
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #include "mlir/Dialect/LLVMIR/ROCDLDialect.h"
27 
28 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
29 #include "mlir/IR/Builders.h"
30 #include "mlir/IR/MLIRContext.h"
31 #include "mlir/IR/Operation.h"
32 #include "mlir/IR/StandardTypes.h"
33 #include "llvm/AsmParser/Parser.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/Function.h"
36 #include "llvm/IR/Type.h"
37 #include "llvm/Support/SourceMgr.h"
38 
39 namespace mlir {
40 namespace ROCDL {
41 
42 //===----------------------------------------------------------------------===//
43 // Printing/parsing for ROCDL ops
44 //===----------------------------------------------------------------------===//
45 
46 static void printROCDLOp(OpAsmPrinter &p, Operation *op) {
47   p << op->getName() << " ";
48   p.printOperands(op->getOperands());
49   if (op->getNumResults() > 0)
50     interleaveComma(op->getResultTypes(), p << " : ");
51 }
52 
53 // <operation> ::= `rocdl.XYZ` : type
54 static ParseResult parseROCDLOp(OpAsmParser &parser, OperationState &result) {
55   Type type;
56   return failure(parser.parseOptionalAttrDict(result.attributes) ||
57                  parser.parseColonType(type) ||
58                  parser.addTypeToList(type, result.types));
59 }
60 
61 //===----------------------------------------------------------------------===//
62 // ROCDLDialect initialization, type parsing, and registration.
63 //===----------------------------------------------------------------------===//
64 
65 // TODO(herhut): This should be the llvm.rocdl dialect once this is supported.
66 ROCDLDialect::ROCDLDialect(MLIRContext *context) : Dialect("rocdl", context) {
67   addOperations<
68 #define GET_OP_LIST
69 #include "mlir/Dialect/LLVMIR/ROCDLOps.cpp.inc"
70       >();
71 
72   // Support unknown operations because not all ROCDL operations are registered.
73   allowUnknownOperations();
74 }
75 
76 #define GET_OP_CLASSES
77 #include "mlir/Dialect/LLVMIR/ROCDLOps.cpp.inc"
78 
79 static DialectRegistration<ROCDLDialect> rocdlDialect;
80 
81 } // namespace ROCDL
82 } // namespace mlir
83