Skip to content

Commit e5f50bd

Browse files
committed
[mlir][llvm] Move the LLVM dialect definition (NFC).
The revision separates out the LLVM dialect definition in a separate tablegen file and ensures the LLVMOpBase.td can include the attributes defined by LLVMAttrDefs.td. The change allows us to use LLVM dialect attributes in the definition of the intrinsic and memory operation base classes, e.g. to represent alias analysis metadata using attributes. Reviewed By: Dinistro Differential Revision: https://reviews.llvm.org/D148007
1 parent 104cd74 commit e5f50bd

File tree

6 files changed

+167
-159
lines changed

6 files changed

+167
-159
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#ifndef LLVMIR_ATTRDEFS
1010
#define LLVMIR_ATTRDEFS
1111

12+
include "mlir/Dialect/LLVMIR/LLVMDialect.td"
1213
include "mlir/IR/AttrTypeBase.td"
13-
include "mlir/Dialect/LLVMIR/LLVMEnums.td"
14-
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
1514

1615
// All of the attributes will extend this class.
1716
class LLVM_Attr<string name, string attrMnemonic,
@@ -30,15 +29,6 @@ def CConvAttr : LLVM_Attr<"CConv", "cconv"> {
3029
let assemblyFormat = "`<` $CallingConv `>`";
3130
}
3231

33-
//===----------------------------------------------------------------------===//
34-
// FastmathFlagsAttr
35-
//===----------------------------------------------------------------------===//
36-
37-
def LLVM_FastmathFlagsAttr :
38-
EnumAttr<LLVM_Dialect, FastmathFlags, "fastmath"> {
39-
let assemblyFormat = "`<` $value `>`";
40-
}
41-
4232
//===----------------------------------------------------------------------===//
4333
// LinkageAttr
4434
//===----------------------------------------------------------------------===//
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//===-- LLVMDialect.td - LLVM IR dialect definition --------*- tablegen -*-===//
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+
#ifndef LLVMIR_DIALECT
10+
#define LLVMIR_DIALECT
11+
12+
include "mlir/IR/DialectBase.td"
13+
14+
def LLVM_Dialect : Dialect {
15+
let name = "llvm";
16+
let cppNamespace = "::mlir::LLVM";
17+
18+
let useDefaultAttributePrinterParser = 1;
19+
let hasRegionArgAttrVerify = 1;
20+
let hasRegionResultAttrVerify = 1;
21+
let hasOperationAttrVerify = 1;
22+
23+
let extraClassDeclaration = [{
24+
/// Name of the data layout attributes.
25+
static StringRef getDataLayoutAttrName() { return "llvm.data_layout"; }
26+
static StringRef getNoAliasScopesAttrName() { return "noalias_scopes"; }
27+
static StringRef getAliasScopesAttrName() { return "alias_scopes"; }
28+
static StringRef getAccessGroupsAttrName() { return "access_groups"; }
29+
30+
/// Names of llvm parameter attributes.
31+
static StringRef getAlignAttrName() { return "llvm.align"; }
32+
static StringRef getAllocAlignAttrName() { return "llvm.allocalign"; }
33+
static StringRef getAllocatedPointerAttrName() { return "llvm.allocptr"; }
34+
static StringRef getByValAttrName() { return "llvm.byval"; }
35+
static StringRef getByRefAttrName() { return "llvm.byref"; }
36+
static StringRef getNoUndefAttrName() { return "llvm.noundef"; }
37+
static StringRef getDereferenceableAttrName() { return "llvm.dereferenceable"; }
38+
static StringRef getDereferenceableOrNullAttrName() { return "llvm.dereferenceable_or_null"; }
39+
static StringRef getInAllocaAttrName() { return "llvm.inalloca"; }
40+
static StringRef getInRegAttrName() { return "llvm.inreg"; }
41+
static StringRef getNestAttrName() { return "llvm.nest"; }
42+
static StringRef getNoAliasAttrName() { return "llvm.noalias"; }
43+
static StringRef getNoCaptureAttrName() { return "llvm.nocapture"; }
44+
static StringRef getNoFreeAttrName() { return "llvm.nofree"; }
45+
static StringRef getNonNullAttrName() { return "llvm.nonnull"; }
46+
static StringRef getPreallocatedAttrName() { return "llvm.preallocated"; }
47+
static StringRef getReadonlyAttrName() { return "llvm.readonly"; }
48+
static StringRef getReturnedAttrName() { return "llvm.returned"; }
49+
static StringRef getSExtAttrName() { return "llvm.signext"; }
50+
static StringRef getStackAlignmentAttrName() { return "llvm.alignstack"; }
51+
static StringRef getStructRetAttrName() { return "llvm.sret"; }
52+
static StringRef getWriteOnlyAttrName() { return "llvm.writeonly"; }
53+
static StringRef getZExtAttrName() { return "llvm.zeroext"; }
54+
// TODO Restrict the usage of this to parameter attributes once there is an
55+
// alternative way of modeling memory effects on FunctionOpInterface.
56+
/// Name of the attribute that will cause the creation of a readnone memory
57+
/// effect when lowering to the LLVMDialect.
58+
static StringRef getReadnoneAttrName() { return "llvm.readnone"; }
59+
60+
/// Verifies if the given string is a well-formed data layout descriptor.
61+
/// Uses `reportError` to report errors.
62+
static LogicalResult verifyDataLayoutString(
63+
StringRef descr, llvm::function_ref<void (const Twine &)> reportError);
64+
65+
/// Name of the target triple attribute.
66+
static StringRef getTargetTripleAttrName() { return "llvm.target_triple"; }
67+
68+
/// Name of the C wrapper emission attribute.
69+
static StringRef getEmitCWrapperAttrName() {
70+
return "llvm.emit_c_interface";
71+
}
72+
73+
/// Returns `true` if the given type is compatible with the LLVM dialect.
74+
static bool isCompatibleType(Type);
75+
76+
77+
Type parseType(DialectAsmParser &p) const override;
78+
void printType(Type, DialectAsmPrinter &p) const override;
79+
80+
private:
81+
/// Verifies a parameter attribute attached to a parameter of type
82+
/// paramType.
83+
LogicalResult verifyParameterAttribute(Operation *op,
84+
Type paramType,
85+
NamedAttribute paramAttr);
86+
87+
/// Register all types.
88+
void registerTypes();
89+
90+
/// A cache storing compatible LLVM types that have been verified. This
91+
/// can save us lots of verification time if there are many occurrences
92+
/// of some deeply-nested aggregate types in the program.
93+
ThreadLocalCache<DenseSet<Type>> compatibleTypes;
94+
95+
/// Register the attributes of this dialect.
96+
void registerAttributes();
97+
}];
98+
}
99+
100+
#endif // LLVMIR_DIALECT

mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,64 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVMIR_ENUMS_TD
10-
#define LLVMIR_ENUMS_TD
9+
#ifndef LLVMIR_ENUMS
10+
#define LLVMIR_ENUMS
1111

12-
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
12+
include "mlir/Dialect/LLVMIR/LLVMDialect.td"
13+
include "mlir/IR/EnumAttr.td"
14+
15+
//===----------------------------------------------------------------------===//
16+
// Base classes for LLVM enum attributes.
17+
//===----------------------------------------------------------------------===//
18+
19+
// Case of the LLVM enum attribute backed by I64Attr with customized string
20+
// representation that corresponds to what is visible in the textual IR form.
21+
// The parameters are as follows:
22+
// - `cppSym`: name of the C++ enumerant for this case in MLIR API;
23+
// - `irSym`: keyword used in the custom form of MLIR operation;
24+
// - `llvmSym`: name of the C++ enumerant for this case in LLVM API.
25+
// For example, `LLVM_EnumAttrCase<"Weak", "weak", "WeakAnyLinkage">` is usable
26+
// as `<MlirEnumName>::Weak` in MLIR API, `WeakAnyLinkage` in LLVM API and
27+
// is printed/parsed as `weak` in MLIR custom textual format.
28+
class LLVM_EnumAttrCase<string cppSym, string irSym, string llvmSym, int val> :
29+
I64EnumAttrCase<cppSym, val, irSym> {
30+
// The name of the equivalent enumerant in LLVM.
31+
string llvmEnumerant = llvmSym;
32+
}
33+
34+
// LLVM enum attribute backed by I64Attr with string representation
35+
// corresponding to what is visible in the textual IR form.
36+
// The parameters are as follows:
37+
// - `name`: name of the C++ enum class in MLIR API;
38+
// - `llvmName`: name of the C++ enum in LLVM API;
39+
// - `description`: textual description for documentation purposes;
40+
// - `cases`: list of enum cases;
41+
// - `unsupportedCases`: optional list of unsupported enum cases.
42+
// For example, `LLVM_EnumAttr<Linkage, "::llvm::GlobalValue::LinkageTypes`
43+
// produces `mlir::LLVM::Linkage` enum class in MLIR API that corresponds to (a
44+
// subset of) values in the `llvm::GlobalValue::LinkageTypes` in LLVM API.
45+
// All unsupported cases are excluded from the MLIR enum and trigger an error
46+
// during the import from LLVM IR. They are useful to handle sentinel values
47+
// such as `llvm::AtomicRMWInst::BinOp::BAD_BINOP` that LLVM commonly uses to
48+
// terminate its enums.
49+
class LLVM_EnumAttr<string name, string llvmName, string description,
50+
list<LLVM_EnumAttrCase> cases,
51+
list<LLVM_EnumAttrCase> unsupportedCases = []> :
52+
I64EnumAttr<name, description, cases> {
53+
// List of unsupported cases that have no conversion to an MLIR value.
54+
list<LLVM_EnumAttrCase> unsupported = unsupportedCases;
55+
56+
// The equivalent enum class name in LLVM.
57+
string llvmClassName = llvmName;
58+
}
59+
60+
// LLVM_CEnumAttr is functionally identical to LLVM_EnumAttr, but to be used for
61+
// non-class enums.
62+
class LLVM_CEnumAttr<string name, string llvmNS, string description,
63+
list<LLVM_EnumAttrCase> cases> :
64+
I64EnumAttr<name, description, cases> {
65+
string llvmClassName = llvmNS;
66+
}
1367

1468
//===----------------------------------------------------------------------===//
1569
// AsmDialect
@@ -402,6 +456,11 @@ def FastmathFlags : I32BitEnumAttr<
402456
let printBitEnumPrimaryGroups = 1;
403457
}
404458

459+
def LLVM_FastmathFlagsAttr :
460+
EnumAttr<LLVM_Dialect, FastmathFlags, "fastmath"> {
461+
let assemblyFormat = "`<` $value `>`";
462+
}
463+
405464
//===----------------------------------------------------------------------===//
406465
// FCmp and ICmp Predicates
407466
//===----------------------------------------------------------------------===//
@@ -582,4 +641,4 @@ def ModRefInfoEnum : LLVM_EnumAttr<
582641
let cppNamespace = "::mlir::LLVM";
583642
}
584643

585-
#endif // LLVMIR_ENUMS_TD
644+
#endif // LLVMIR_ENUMS

mlir/include/mlir/Dialect/LLVMIR/LLVMIntrinsicOps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
include "mlir/IR/OpBase.td"
55
include "mlir/Dialect/LLVMIR/LLVMAttrDefs.td"
6+
include "mlir/Dialect/LLVMIR/LLVMEnums.td"
67
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
78
include "mlir/Interfaces/InferTypeOpInterface.td"
89

mlir/include/mlir/Dialect/LLVMIR/LLVMOpBase.td

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -14,101 +14,11 @@
1414
#ifndef LLVMIR_OP_BASE
1515
#define LLVMIR_OP_BASE
1616

17+
include "mlir/Dialect/LLVMIR/LLVMAttrDefs.td"
1718
include "mlir/Dialect/LLVMIR/LLVMInterfaces.td"
18-
include "mlir/IR/EnumAttr.td"
1919
include "mlir/IR/OpBase.td"
2020
include "mlir/Interfaces/SideEffectInterfaces.td"
2121

22-
//===----------------------------------------------------------------------===//
23-
// LLVM Dialect.
24-
//===----------------------------------------------------------------------===//
25-
26-
def LLVM_Dialect : Dialect {
27-
let name = "llvm";
28-
let cppNamespace = "::mlir::LLVM";
29-
30-
let useDefaultAttributePrinterParser = 1;
31-
let hasRegionArgAttrVerify = 1;
32-
let hasRegionResultAttrVerify = 1;
33-
let hasOperationAttrVerify = 1;
34-
35-
let extraClassDeclaration = [{
36-
/// Name of the data layout attributes.
37-
static StringRef getDataLayoutAttrName() { return "llvm.data_layout"; }
38-
static StringRef getNoAliasScopesAttrName() { return "noalias_scopes"; }
39-
static StringRef getAliasScopesAttrName() { return "alias_scopes"; }
40-
static StringRef getAccessGroupsAttrName() { return "access_groups"; }
41-
42-
/// Names of llvm parameter attributes.
43-
static StringRef getAlignAttrName() { return "llvm.align"; }
44-
static StringRef getAllocAlignAttrName() { return "llvm.allocalign"; }
45-
static StringRef getAllocatedPointerAttrName() { return "llvm.allocptr"; }
46-
static StringRef getByValAttrName() { return "llvm.byval"; }
47-
static StringRef getByRefAttrName() { return "llvm.byref"; }
48-
static StringRef getNoUndefAttrName() { return "llvm.noundef"; }
49-
static StringRef getDereferenceableAttrName() { return "llvm.dereferenceable"; }
50-
static StringRef getDereferenceableOrNullAttrName() { return "llvm.dereferenceable_or_null"; }
51-
static StringRef getInAllocaAttrName() { return "llvm.inalloca"; }
52-
static StringRef getInRegAttrName() { return "llvm.inreg"; }
53-
static StringRef getNestAttrName() { return "llvm.nest"; }
54-
static StringRef getNoAliasAttrName() { return "llvm.noalias"; }
55-
static StringRef getNoCaptureAttrName() { return "llvm.nocapture"; }
56-
static StringRef getNoFreeAttrName() { return "llvm.nofree"; }
57-
static StringRef getNonNullAttrName() { return "llvm.nonnull"; }
58-
static StringRef getPreallocatedAttrName() { return "llvm.preallocated"; }
59-
static StringRef getReadonlyAttrName() { return "llvm.readonly"; }
60-
static StringRef getReturnedAttrName() { return "llvm.returned"; }
61-
static StringRef getSExtAttrName() { return "llvm.signext"; }
62-
static StringRef getStackAlignmentAttrName() { return "llvm.alignstack"; }
63-
static StringRef getStructRetAttrName() { return "llvm.sret"; }
64-
static StringRef getWriteOnlyAttrName() { return "llvm.writeonly"; }
65-
static StringRef getZExtAttrName() { return "llvm.zeroext"; }
66-
// TODO Restrict the usage of this to parameter attributes once there is an
67-
// alternative way of modeling memory effects on FunctionOpInterface.
68-
/// Name of the attribute that will cause the creation of a readnone memory
69-
/// effect when lowering to the LLVMDialect.
70-
static StringRef getReadnoneAttrName() { return "llvm.readnone"; }
71-
72-
/// Verifies if the given string is a well-formed data layout descriptor.
73-
/// Uses `reportError` to report errors.
74-
static LogicalResult verifyDataLayoutString(
75-
StringRef descr, llvm::function_ref<void (const Twine &)> reportError);
76-
77-
/// Name of the target triple attribute.
78-
static StringRef getTargetTripleAttrName() { return "llvm.target_triple"; }
79-
80-
/// Name of the C wrapper emission attribute.
81-
static StringRef getEmitCWrapperAttrName() {
82-
return "llvm.emit_c_interface";
83-
}
84-
85-
/// Returns `true` if the given type is compatible with the LLVM dialect.
86-
static bool isCompatibleType(Type);
87-
88-
89-
Type parseType(DialectAsmParser &p) const override;
90-
void printType(Type, DialectAsmPrinter &p) const override;
91-
92-
private:
93-
/// Verifies a parameter attribute attached to a parameter of type
94-
/// paramType.
95-
LogicalResult verifyParameterAttribute(Operation *op,
96-
Type paramType,
97-
NamedAttribute paramAttr);
98-
99-
/// Register all types.
100-
void registerTypes();
101-
102-
/// A cache storing compatible LLVM types that have been verified. This
103-
/// can save us lots of verification time if there are many occurrences
104-
/// of some deeply-nested aggregate types in the program.
105-
ThreadLocalCache<DenseSet<Type>> compatibleTypes;
106-
107-
/// Register the attributes of this dialect.
108-
void registerAttributes();
109-
}];
110-
}
111-
11222
//===----------------------------------------------------------------------===//
11323
// LLVM dialect type constraints.
11424
//===----------------------------------------------------------------------===//
@@ -268,59 +178,6 @@ class LLVM_OpBase<Dialect dialect, string mnemonic, list<Trait> traits = []> :
268178
list<int> llvmArgIndices = [];
269179
}
270180

271-
//===----------------------------------------------------------------------===//
272-
// Base classes for LLVM enum attributes.
273-
//===----------------------------------------------------------------------===//
274-
275-
// Case of the LLVM enum attribute backed by I64Attr with customized string
276-
// representation that corresponds to what is visible in the textual IR form.
277-
// The parameters are as follows:
278-
// - `cppSym`: name of the C++ enumerant for this case in MLIR API;
279-
// - `irSym`: keyword used in the custom form of MLIR operation;
280-
// - `llvmSym`: name of the C++ enumerant for this case in LLVM API.
281-
// For example, `LLVM_EnumAttrCase<"Weak", "weak", "WeakAnyLinkage">` is usable
282-
// as `<MlirEnumName>::Weak` in MLIR API, `WeakAnyLinkage` in LLVM API and
283-
// is printed/parsed as `weak` in MLIR custom textual format.
284-
class LLVM_EnumAttrCase<string cppSym, string irSym, string llvmSym, int val> :
285-
I64EnumAttrCase<cppSym, val, irSym> {
286-
// The name of the equivalent enumerant in LLVM.
287-
string llvmEnumerant = llvmSym;
288-
}
289-
290-
// LLVM enum attribute backed by I64Attr with string representation
291-
// corresponding to what is visible in the textual IR form.
292-
// The parameters are as follows:
293-
// - `name`: name of the C++ enum class in MLIR API;
294-
// - `llvmName`: name of the C++ enum in LLVM API;
295-
// - `description`: textual description for documentation purposes;
296-
// - `cases`: list of enum cases;
297-
// - `unsupportedCases`: optional list of unsupported enum cases.
298-
// For example, `LLVM_EnumAttr<Linkage, "::llvm::GlobalValue::LinkageTypes`
299-
// produces `mlir::LLVM::Linkage` enum class in MLIR API that corresponds to (a
300-
// subset of) values in the `llvm::GlobalValue::LinkageTypes` in LLVM API.
301-
// All unsupported cases are excluded from the MLIR enum and trigger an error
302-
// during the import from LLVM IR. They are useful to handle sentinel values
303-
// such as `llvm::AtomicRMWInst::BinOp::BAD_BINOP` that LLVM commonly uses to
304-
// terminate its enums.
305-
class LLVM_EnumAttr<string name, string llvmName, string description,
306-
list<LLVM_EnumAttrCase> cases,
307-
list<LLVM_EnumAttrCase> unsupportedCases = []> :
308-
I64EnumAttr<name, description, cases> {
309-
// List of unsupported cases that have no conversion to an MLIR value.
310-
list<LLVM_EnumAttrCase> unsupported = unsupportedCases;
311-
312-
// The equivalent enum class name in LLVM.
313-
string llvmClassName = llvmName;
314-
}
315-
316-
// LLVM_CEnumAttr is functionally identical to LLVM_EnumAttr, but to be used for
317-
// non-class enums.
318-
class LLVM_CEnumAttr<string name, string llvmNS, string description,
319-
list<LLVM_EnumAttrCase> cases> :
320-
I64EnumAttr<name, description, cases> {
321-
string llvmClassName = llvmNS;
322-
}
323-
324181
//===----------------------------------------------------------------------===//
325182
// Patterns for LLVM dialect operations.
326183
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVMIR_OPS
1515

1616
include "mlir/Dialect/LLVMIR/LLVMAttrDefs.td"
17+
include "mlir/Dialect/LLVMIR/LLVMEnums.td"
1718
include "mlir/Dialect/LLVMIR/LLVMOpBase.td"
1819
include "mlir/IR/EnumAttr.td"
1920
include "mlir/IR/FunctionInterfaces.td"

0 commit comments

Comments
 (0)