Skip to content

Commit 85de7d1

Browse files
committed
[MLIR][LLVM] Add bytecode support for several attributes
1 parent 04799bb commit 85de7d1

File tree

9 files changed

+621
-2
lines changed

9 files changed

+621
-2
lines changed

mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ mlir_tablegen(LLVMIntrinsicFromLLVMIRConversions.inc -gen-intr-from-llvmir-conve
4848
mlir_tablegen(LLVMConvertibleLLVMIRIntrinsics.inc -gen-convertible-llvmir-intrinsics)
4949
add_mlir_dialect_tablegen_target(MLIRLLVMIntrinsicConversionsIncGen)
5050

51+
set(LLVM_TARGET_DEFINITIONS LLVMDialectBytecode.td)
52+
mlir_tablegen(LLVMDialectBytecode.cpp.inc -gen-bytecode -bytecode-dialect="LLVM")
53+
add_public_tablegen_target(MLIRLLVMDialectBytecodeIncGen)
54+
5155
set(LLVM_TARGET_DEFINITIONS BasicPtxBuilderInterface.td)
5256
mlir_tablegen(BasicPtxBuilderInterface.h.inc -gen-op-interface-decls)
5357
mlir_tablegen(BasicPtxBuilderInterface.cpp.inc -gen-op-interface-defs)
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
//===-- LLVMDialectBytecode.td - LLVM bytecode defs --------*- 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+
// This is the LLVM bytecode reader/writer definition file.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_DIALECT_BYTECODE
14+
#define LLVM_DIALECT_BYTECODE
15+
16+
include "mlir/IR/BytecodeBase.td"
17+
18+
//===----------------------------------------------------------------------===//
19+
// Bytecode classes for attributes and types.
20+
//===----------------------------------------------------------------------===//
21+
22+
def String :
23+
WithParser <"succeeded($_reader.readString($_var))",
24+
WithBuilder<"$_args",
25+
WithPrinter<"$_writer.writeOwnedString($_getter)",
26+
WithType <"StringRef">>>>;
27+
28+
class Attr<string type> : WithType<type, Attribute>;
29+
30+
class OptionalAttribute<string type> :
31+
WithParser <"succeeded($_reader.readOptionalAttribute($_var))",
32+
WithPrinter<"$_writer.writeOptionalAttribute($_getter)",
33+
WithType<type, Attribute>>>;
34+
35+
class OptionalInt<string type> :
36+
WithParser <"succeeded(readOptionalInt($_reader, $_var))",
37+
WithPrinter<"writeOptionalInt($_writer, $_getter)",
38+
WithType<"std::optional<" # type # ">", VarInt>>>;
39+
40+
class OptionalArrayRef<string eltType> :
41+
WithParser <"succeeded(readOptionalArrayRef<"
42+
# eltType # ">($_reader, $_var))",
43+
WithPrinter<"writeOptionalArrayRef<"
44+
# eltType # ">($_writer, $_getter)",
45+
WithType<"SmallVector<"
46+
# eltType # ">", Attribute>>>;
47+
48+
class EnumClassFlag<string flag, string getter> :
49+
WithParser<"succeeded($_reader.readVarInt($_var))",
50+
WithBuilder<"(" # flag # ")$_args",
51+
WithPrinter<"$_writer.writeVarInt((uint64_t)$_name." # getter # ")",
52+
WithType<"uint64_t", VarInt>>>>;
53+
54+
//===----------------------------------------------------------------------===//
55+
// General notes
56+
// - For each attribute or type entry, the argument names should match
57+
// LLVMAttrDefs.td
58+
// - The mnemonics are either LLVM or builtin MLIR attributes and types, but
59+
// regular C++ types are also allowed to match builders and parsers.
60+
//===----------------------------------------------------------------------===//
61+
62+
//===----------------------------------------------------------------------===//
63+
// AliasScopeAttr
64+
//===----------------------------------------------------------------------===//
65+
66+
def AliasScopeAttr : DialectAttribute<(attr
67+
Attr<"Attribute">:$id,
68+
Attr<"AliasScopeDomainAttr">:$domain,
69+
OptionalAttribute<"StringAttr">:$description
70+
)>;
71+
72+
//===----------------------------------------------------------------------===//
73+
// DIBasicTypeAttr
74+
//===----------------------------------------------------------------------===//
75+
76+
def DIBasicTypeAttr : DialectAttribute<(attr
77+
VarInt:$tag,
78+
String:$name,
79+
VarInt:$sizeInBits,
80+
VarInt:$encoding
81+
)>;
82+
83+
//===----------------------------------------------------------------------===//
84+
// DIExpressionAttr, DIExpressionElemAttr
85+
//===----------------------------------------------------------------------===//
86+
87+
def DIExpressionElemAttr : DialectAttribute<(attr
88+
VarInt:$opcode,
89+
OptionalArrayRef<"uint64_t">:$arguments
90+
)>;
91+
92+
def DIExpressionAttr : DialectAttribute<(attr
93+
OptionalArrayRef<"DIExpressionElemAttr">:$operations
94+
)>;
95+
96+
//===----------------------------------------------------------------------===//
97+
// DIFileAttr
98+
//===----------------------------------------------------------------------===//
99+
100+
def DIFileAttr : DialectAttribute<(attr
101+
String:$name,
102+
String:$directory
103+
)>;
104+
105+
//===----------------------------------------------------------------------===//
106+
// DILocalVariableAttr
107+
//===----------------------------------------------------------------------===//
108+
109+
def DILocalVariableAttr : DialectAttribute<(attr
110+
Attr<"DIScopeAttr">:$scope, // Non-optional attribute
111+
OptionalAttribute<"StringAttr">:$name,
112+
OptionalAttribute<"DIFileAttr">:$file,
113+
VarInt:$line,
114+
VarInt:$arg,
115+
VarInt:$alignInBits,
116+
OptionalAttribute<"DITypeAttr">:$type,
117+
EnumClassFlag<"DIFlags", "getFlags()">:$_rawflags,
118+
LocalVar<"DIFlags", "(DIFlags)_rawflags">:$flags
119+
)> {
120+
// DILocalVariableAttr direct getter uses a `StringRef` for `name`. Since the
121+
// more direct getter is prefered during bytecode reading, force the base one
122+
// and prevent crashes for empty `StringAttr`.
123+
let cBuilder = "$_resultType::get(context, $_args)";
124+
}
125+
126+
//===----------------------------------------------------------------------===//
127+
// DISubroutineTypeAttr
128+
//===----------------------------------------------------------------------===//
129+
130+
def DISubroutineTypeAttr : DialectAttribute<(attr
131+
VarInt:$callingConvention,
132+
OptionalArrayRef<"DITypeAttr">:$types
133+
)>;
134+
135+
//===----------------------------------------------------------------------===//
136+
// DICompileUnitAttr
137+
//===----------------------------------------------------------------------===//
138+
139+
def DICompileUnitAttr : DialectAttribute<(attr
140+
Attr<"DistinctAttr">:$id,
141+
VarInt:$sourceLanguage,
142+
Attr<"DIFileAttr">:$file,
143+
OptionalAttribute<"StringAttr">:$producer,
144+
Bool:$isOptimized,
145+
EnumClassFlag<"DIEmissionKind", "getEmissionKind()">:$_rawEmissionKind,
146+
LocalVar<"DIEmissionKind", "(DIEmissionKind)_rawEmissionKind">:$emissionKind,
147+
EnumClassFlag<"DINameTableKind", "getNameTableKind()">:$_rawNameTableKind,
148+
LocalVar<"DINameTableKind",
149+
"(DINameTableKind)_rawNameTableKind">:$nameTableKind
150+
)>;
151+
152+
//===----------------------------------------------------------------------===//
153+
// DISubprogramAttr
154+
//===----------------------------------------------------------------------===//
155+
156+
def DISubprogramAttr : DialectAttribute<(attr
157+
OptionalAttribute<"DistinctAttr">:$recId,
158+
Bool:$isRecSelf,
159+
OptionalAttribute<"DistinctAttr">:$id,
160+
OptionalAttribute<"DICompileUnitAttr">:$compileUnit,
161+
OptionalAttribute<"DIScopeAttr">:$scope, // TODO: DIScopeAttr
162+
OptionalAttribute<"StringAttr">:$name,
163+
OptionalAttribute<"StringAttr">:$linkageName,
164+
OptionalAttribute<"DIFileAttr">:$file,
165+
VarInt:$line,
166+
VarInt:$scopeLine,
167+
EnumClassFlag<"DISubprogramFlags", "getSubprogramFlags()">:$_rawflags,
168+
LocalVar<"DISubprogramFlags", "(DISubprogramFlags)_rawflags">:$subprogramFlags,
169+
OptionalAttribute<"DISubroutineTypeAttr">:$type,
170+
OptionalArrayRef<"DINodeAttr">:$retainedNodes,
171+
OptionalArrayRef<"DINodeAttr">:$annotations
172+
)>;
173+
174+
//===----------------------------------------------------------------------===//
175+
// DICompositeTypeAttr
176+
//===----------------------------------------------------------------------===//
177+
178+
def DICompositeTypeAttr : DialectAttribute<(attr
179+
OptionalAttribute<"DistinctAttr">:$recId,
180+
Bool:$isRecSelf,
181+
VarInt:$tag,
182+
OptionalAttribute<"StringAttr">:$name,
183+
OptionalAttribute<"DIFileAttr">:$file,
184+
VarInt:$line,
185+
OptionalAttribute<"DIScopeAttr">:$scope,
186+
OptionalAttribute<"DITypeAttr">:$baseType,
187+
EnumClassFlag<"DIFlags", "getFlags()">:$_rawflags,
188+
LocalVar<"DIFlags", "(DIFlags)_rawflags">:$flags,
189+
VarInt:$sizeInBits,
190+
VarInt:$alignInBits,
191+
OptionalArrayRef<"DINodeAttr">:$elements,
192+
OptionalAttribute<"DIExpressionAttr">:$dataLocation,
193+
OptionalAttribute<"DIExpressionAttr">:$rank,
194+
OptionalAttribute<"DIExpressionAttr">:$allocated,
195+
OptionalAttribute<"DIExpressionAttr">:$associated
196+
)>;
197+
198+
//===----------------------------------------------------------------------===//
199+
// DIDerivedTypeAttr
200+
//===----------------------------------------------------------------------===//
201+
202+
def DIDerivedTypeAttr : DialectAttribute<(attr
203+
VarInt:$tag,
204+
OptionalAttribute<"StringAttr">:$name,
205+
OptionalAttribute<"DITypeAttr">:$baseType,
206+
VarInt:$sizeInBits,
207+
VarInt:$alignInBits,
208+
VarInt:$offsetInBits,
209+
OptionalInt<"unsigned">:$dwarfAddressSpace,
210+
OptionalAttribute<"DINodeAttr">:$extraData // TODO: DINodeAttr
211+
)>;
212+
213+
//===----------------------------------------------------------------------===//
214+
// DIImportedEntityAttr
215+
//===----------------------------------------------------------------------===//
216+
217+
def DIImportedEntityAttr : DialectAttribute<(attr
218+
VarInt:$tag,
219+
Attr<"DIScopeAttr">:$scope,
220+
Attr<"DINodeAttr">:$entity,
221+
OptionalAttribute<"DIFileAttr">:$file,
222+
VarInt:$line,
223+
OptionalAttribute<"StringAttr">:$name,
224+
OptionalArrayRef<"DINodeAttr">:$elements
225+
)>;
226+
227+
//===----------------------------------------------------------------------===//
228+
// DIGlobalVariableAttr, DIGlobalVariableExpressionAttr
229+
//===----------------------------------------------------------------------===//
230+
231+
def DIGlobalVariableAttr : DialectAttribute<(attr
232+
OptionalAttribute<"DIScopeAttr">:$scope,
233+
OptionalAttribute<"StringAttr">:$name,
234+
OptionalAttribute<"StringAttr">:$linkageName,
235+
Attr<"DIFileAttr">:$file,
236+
VarInt:$line,
237+
Attr<"DITypeAttr">:$type,
238+
Bool:$isLocalToUnit,
239+
Bool:$isDefined,
240+
VarInt:$alignInBits
241+
)>;
242+
243+
def DIGlobalVariableExpressionAttr : DialectAttribute<(attr
244+
Attr<"DIGlobalVariableAttr">:$var,
245+
OptionalAttribute<"DIExpressionAttr">:$expr
246+
)>;
247+
248+
//===----------------------------------------------------------------------===//
249+
// DILabelAttr
250+
//===----------------------------------------------------------------------===//
251+
252+
def DILabelAttr : DialectAttribute<(attr
253+
Attr<"DIScopeAttr">:$scope,
254+
OptionalAttribute<"StringAttr">:$name,
255+
OptionalAttribute<"DIFileAttr">:$file,
256+
VarInt:$line
257+
)> {
258+
// DILabelAttr direct getter uses a `StringRef` for `name`. Since the
259+
// more direct getter is prefered during bytecode reading, force the base one
260+
// and prevent crashes for empty `StringAttr`.
261+
let cBuilder = "$_resultType::get(context, $_args)";
262+
}
263+
264+
//===----------------------------------------------------------------------===//
265+
// DILexicalBlockAttr, DILexicalBlockFileAttr
266+
//===----------------------------------------------------------------------===//
267+
268+
def DILexicalBlockAttr : DialectAttribute<(attr
269+
Attr<"DIScopeAttr">:$scope,
270+
OptionalAttribute<"DIFileAttr">:$file,
271+
VarInt:$line,
272+
VarInt:$column
273+
)>;
274+
275+
def DILexicalBlockFileAttr : DialectAttribute<(attr
276+
Attr<"DIScopeAttr">:$scope,
277+
OptionalAttribute<"DIFileAttr">:$file,
278+
VarInt:$discriminator
279+
)>;
280+
281+
//===----------------------------------------------------------------------===//
282+
// DINamespaceAttr
283+
//===----------------------------------------------------------------------===//
284+
285+
def DINamespaceAttr : DialectAttribute<(attr
286+
OptionalAttribute<"StringAttr">:$name,
287+
OptionalAttribute<"DIScopeAttr">:$scope,
288+
Bool:$exportSymbols
289+
)>;
290+
291+
//===----------------------------------------------------------------------===//
292+
// DISubrangeAttr
293+
//===----------------------------------------------------------------------===//
294+
295+
def DISubrangeAttr : DialectAttribute<(attr
296+
OptionalAttribute<"Attribute">:$count,
297+
OptionalAttribute<"Attribute">:$lowerBound,
298+
OptionalAttribute<"Attribute">:$upperBound,
299+
OptionalAttribute<"Attribute">:$stride
300+
)>;
301+
302+
//===----------------------------------------------------------------------===//
303+
// LoopAnnotationAttr
304+
//===----------------------------------------------------------------------===//
305+
306+
def LoopAnnotationAttr : DialectAttribute<(attr
307+
OptionalAttribute<"BoolAttr">:$disableNonforced,
308+
OptionalAttribute<"LoopVectorizeAttr">:$vectorize, // TODO: LoopVectorizeAttr
309+
OptionalAttribute<"LoopInterleaveAttr">:$interleave, // TODO: LoopInterleaveAttr
310+
OptionalAttribute<"LoopUnrollAttr">:$unroll, // TODO: LoopUnrollAttr
311+
OptionalAttribute<"LoopUnrollAndJamAttr">:$unrollAndJam, // TODO: LoopUnrollAndJamAttr
312+
OptionalAttribute<"LoopLICMAttr">:$licm, // TODO: LoopLICMAttr
313+
OptionalAttribute<"LoopDistributeAttr">:$distribute, // TODO: LoopDistributeAttr
314+
OptionalAttribute<"LoopPipelineAttr">:$pipeline, // TODO: LoopPipelineAttr
315+
OptionalAttribute<"LoopPeeledAttr">:$peeled, // TODO: LoopPeeledAttr
316+
OptionalAttribute<"LoopUnswitchAttr">:$unswitch, // TODO: LoopUnswitchAttr
317+
OptionalAttribute<"BoolAttr">:$mustProgress,
318+
OptionalAttribute<"BoolAttr">:$isVectorized,
319+
OptionalAttribute<"FusedLoc">:$startLoc,
320+
OptionalAttribute<"FusedLoc">:$endLoc,
321+
OptionalArrayRef<"AccessGroupAttr">:$parallelAccesses // TODO: AccessGroupAttr
322+
)>;
323+
324+
//===----------------------------------------------------------------------===//
325+
// Attributes & Types with custom bytecode handling.
326+
//===----------------------------------------------------------------------===//
327+
328+
def LLVMDialectAttributes : DialectAttributes<"LLVM"> {
329+
let elems = [
330+
AliasScopeAttr,
331+
DIBasicTypeAttr,
332+
DICompileUnitAttr,
333+
DICompositeTypeAttr,
334+
DIDerivedTypeAttr,
335+
DIExpressionElemAttr,
336+
DIExpressionAttr,
337+
DIFileAttr,
338+
DIGlobalVariableAttr,
339+
DIGlobalVariableExpressionAttr,
340+
DIImportedEntityAttr,
341+
DILabelAttr,
342+
DILexicalBlockAttr,
343+
DILexicalBlockFileAttr,
344+
DILocalVariableAttr,
345+
DINamespaceAttr,
346+
DISubprogramAttr,
347+
DISubrangeAttr,
348+
DISubroutineTypeAttr,
349+
LoopAnnotationAttr
350+
];
351+
}
352+
353+
def LLVMDialectTypes : DialectTypes<"LLVM"> {
354+
let elems = [];
355+
}
356+
357+
#endif // LLVM_DIALECT_BYTECODE

mlir/lib/Dialect/LLVMIR/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ add_mlir_dialect_library(MLIRLLVMDialect
88
IR/LLVMMemorySlot.cpp
99
IR/LLVMTypes.cpp
1010
IR/LLVMTypeSyntax.cpp
11+
IR/LLVMDialectBytecode.cpp
1112

1213
ADDITIONAL_HEADER_DIRS
1314
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR
1415

1516
DEPENDS
17+
MLIRLLVMDialectBytecodeIncGen
1618
MLIRLLVMOpsIncGen
1719
MLIRLLVMTypesIncGen
1820
MLIRLLVMIntrinsicOpsIncGen

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "llvm/IR/DataLayout.h"
3030
#include "llvm/Support/Error.h"
3131

32+
#include "LLVMDialectBytecode.h"
33+
3234
#include <numeric>
3335
#include <optional>
3436

@@ -4237,6 +4239,7 @@ void LLVMDialect::initialize() {
42374239
// Support unknown operations because not all LLVM operations are registered.
42384240
allowUnknownOperations();
42394241
declarePromisedInterface<DialectInlinerInterface, LLVMDialect>();
4242+
detail::addBytecodeInterface(this);
42404243
}
42414244

42424245
#define GET_OP_CLASSES

0 commit comments

Comments
 (0)