Skip to content

Commit 129cf84

Browse files
committed
[mlir] LLVM dialect: support globals without linkage keyword, assuming 'external'
Similarly to actual LLVM IR, and to `llvm.mlir.func`, allow the custom syntax of `llvm.mlir.global` to omit the linkage keyword. If omitted, the linkage is assumed to be external. This makes the modeling of globals in the LLVM dialect more consistent, both within the dialect and with LLVM IR. Differential Revision: https://reviews.llvm.org/D78096
1 parent 04b5274 commit 129cf84

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

mlir/docs/Dialects/LLVM.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ llvm.func @func() attributes {
105105
If the attribute is not known to LLVM IR, it will be attached as a string
106106
attribute.
107107

108+
#### Linkage
109+
110+
An LLVM IR dialect function has a linkage attribute derived from LLVM IR
111+
[linkage types](https://llvm.org/docs/LangRef.html#linkage-types). Linkage is
112+
specified by the same keyword as in LLVM IR and is located between `llvm.func`
113+
and the symbol name. If no linkage keyword is present, `external` linkage is
114+
assumed by default.
115+
108116
### LLVM IR operations
109117

110118
The following operations are currently supported. The semantics of these
@@ -423,6 +431,21 @@ llvm.mlir.global constant @int_gep() : !llvm<"i32*"> {
423431
}
424432
```
425433

434+
Similarly to functions, globals have a linkage attribute. In the custom syntax,
435+
this attribute is placed between `llvm.mlir.global` and the optional `constant`
436+
keyword. If the attribute is omitted, `external` linkage is assumed by default.
437+
438+
Examples:
439+
440+
```mlir
441+
// A constant with internal linkage will not participate in linking.
442+
llvm.mlir.global internal constant @cst(42 : i32) : !llvm.i32
443+
444+
// By default, "external" linkage is assumed and the global participates in
445+
// symbol resolution at link-time.
446+
llvm.mlir.global @glob(0 : f32) : !llvm.float
447+
```
448+
426449
#### `llvm.mlir.null`
427450

428451
Unlike LLVM IR, MLIR does not have first-class null pointers. They must be

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,15 +1010,17 @@ static ParseResult parseOptionalLLVMKeyword(OpAsmParser &parser,
10101010
return success();
10111011
}
10121012

1013-
// operation ::= `llvm.mlir.global` linkage `constant`? `@` identifier
1013+
// operation ::= `llvm.mlir.global` linkage? `constant`? `@` identifier
10141014
// `(` attribute? `)` attribute-list? (`:` type)? region?
10151015
//
10161016
// The type can be omitted for string attributes, in which case it will be
10171017
// inferred from the value of the string as [strlen(value) x i8].
10181018
static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) {
10191019
if (failed(parseOptionalLLVMKeyword<Linkage>(parser, result,
10201020
getLinkageAttrName())))
1021-
return parser.emitError(parser.getCurrentLocation(), "expected linkage");
1021+
result.addAttribute(getLinkageAttrName(),
1022+
parser.getBuilder().getI64IntegerAttr(
1023+
static_cast<int64_t>(LLVM::Linkage::External)));
10221024

10231025
if (succeeded(parser.parseOptionalKeyword("constant")))
10241026
result.addAttribute("constant", parser.getBuilder().getUnitAttr());

mlir/test/Dialect/LLVMIR/global.mlir

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// RUN: mlir-opt -split-input-file -verify-diagnostics %s | FileCheck %s
22

3+
// CHECK: llvm.mlir.global external @default_external
4+
llvm.mlir.global @default_external() : !llvm.i64
5+
6+
// CHECK: llvm.mlir.global external constant @default_external_constant
7+
llvm.mlir.global constant @default_external_constant(42) : !llvm.i64
8+
39
// CHECK: llvm.mlir.global internal @global(42 : i64) : !llvm.i64
410
llvm.mlir.global internal @global(42 : i64) : !llvm.i64
511

0 commit comments

Comments
 (0)