MLIR  14.0.0git
ROCDLDialect.cpp
Go to the documentation of this file.
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 
18 
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`
43  OperationState &result) {
45  Type type;
46  if (parser.parseOperandList(ops, 5) || parser.parseColonType(type) ||
47  parser.addTypeToList(type, result.types))
48  return failure();
49 
50  MLIRContext *context = parser.getContext();
51  auto int32Ty = IntegerType::get(context, 32);
52  auto int1Ty = IntegerType::get(context, 1);
53  auto i32x4Ty = LLVM::getFixedVectorType(int32Ty, 4);
54  return parser.resolveOperands(ops,
55  {i32x4Ty, int32Ty, int32Ty, int1Ty, int1Ty},
56  parser.getNameLoc(), result.operands);
57 }
58 
59 // <operation> ::=
60 // `llvm.amdgcn.buffer.store.* %vdata, %rsrc, %vindex, %offset, %glc, %slc :
61 // result_type`
63  OperationState &result) {
65  Type type;
66  if (parser.parseOperandList(ops, 6) || parser.parseColonType(type))
67  return failure();
68 
69  MLIRContext *context = parser.getContext();
70  auto int32Ty = IntegerType::get(context, 32);
71  auto int1Ty = IntegerType::get(context, 1);
72  auto i32x4Ty = LLVM::getFixedVectorType(int32Ty, 4);
73 
74  if (parser.resolveOperands(ops,
75  {type, i32x4Ty, int32Ty, int32Ty, int1Ty, int1Ty},
76  parser.getNameLoc(), result.operands))
77  return failure();
78  return success();
79 }
80 
81 //===----------------------------------------------------------------------===//
82 // ROCDLDialect initialization, type parsing, and registration.
83 //===----------------------------------------------------------------------===//
84 
85 // TODO: This should be the llvm.rocdl dialect once this is supported.
86 void ROCDLDialect::initialize() {
87  addOperations<
88 #define GET_OP_LIST
89 #include "mlir/Dialect/LLVMIR/ROCDLOps.cpp.inc"
90  >();
91 
92  // Support unknown operations because not all ROCDL operations are registered.
93  allowUnknownOperations();
94 }
95 
96 LogicalResult ROCDLDialect::verifyOperationAttribute(Operation *op,
97  NamedAttribute attr) {
98  // Kernel function attribute should be attached to functions.
99  if (attr.getName() == ROCDLDialect::getKernelFuncAttrName()) {
100  if (!isa<LLVM::LLVMFuncOp>(op)) {
101  return op->emitError() << "'" << ROCDLDialect::getKernelFuncAttrName()
102  << "' attribute attached to unexpected op";
103  }
104  }
105  return success();
106 }
107 
108 #define GET_OP_CLASSES
109 #include "mlir/Dialect/LLVMIR/ROCDLOps.cpp.inc"
Include the generated interface declarations.
ParseResult resolveOperands(ArrayRef< OperandType > operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
Type getFixedVectorType(Type elementType, unsigned numElements)
Creates an LLVM dialect-compatible type with the given element type and length.
Definition: LLVMTypes.cpp:830
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
static ParseResult parseROCDLMubufLoadOp(OpAsmParser &parser, OperationState &result)
ParseResult addTypeToList(Type type, SmallVectorImpl< Type > &result)
Add the specified type to the end of the specified type list and return success.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it...
virtual llvm::SMLoc getNameLoc() const =0
Return the location of the original name token.
SmallVector< Value, 4 > operands
virtual ParseResult parseOperandList(SmallVectorImpl< OperandType > &result, int requiredOperandCount=-1, Delimiter delimiter=Delimiter::None)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter...
NamedAttribute represents a combination of a name and an Attribute value.
Definition: Attributes.h:137
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
StringAttr getName() const
Return the name of the attribute.
Definition: Attributes.cpp:32
This represents an operation in an abstracted form, suitable for use with the builder APIs...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:55
static ParseResult parseROCDLMubufStoreOp(OpAsmParser &parser, OperationState &result)
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
Definition: Operation.cpp:231
MLIRContext * getContext() const
Definition: AsmPrinter.cpp:61
This class represents success/failure for operation parsing.
Definition: OpDefinition.h:36
virtual ParseResult parseColonType(Type &result)=0
Parse a colon followed by a type.
SmallVector< Type, 4 > types
Types of the results of this operation.