[PATCH] D78096: [mlir] LLVM dialect: support globals without linkage keyword, assuming 'external'

Alex Zinenko via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 15 02:09:07 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG129cf84e6953: [mlir] LLVM dialect: support globals without linkage keyword, assuming… (authored by ftynse).

Changed prior to commit:
  https://reviews.llvm.org/D78096?vs=257275&id=257631#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D78096/new/

https://reviews.llvm.org/D78096

Files:
  mlir/docs/Dialects/LLVM.md
  mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
  mlir/test/Dialect/LLVMIR/global.mlir


Index: mlir/test/Dialect/LLVMIR/global.mlir
===================================================================
--- mlir/test/Dialect/LLVMIR/global.mlir
+++ mlir/test/Dialect/LLVMIR/global.mlir
@@ -1,5 +1,11 @@
 // RUN: mlir-opt -split-input-file -verify-diagnostics %s | FileCheck %s
 
+// CHECK: llvm.mlir.global external @default_external
+llvm.mlir.global @default_external() : !llvm.i64
+
+// CHECK: llvm.mlir.global external constant @default_external_constant
+llvm.mlir.global constant @default_external_constant(42) : !llvm.i64
+
 // CHECK: llvm.mlir.global internal @global(42 : i64) : !llvm.i64
 llvm.mlir.global internal @global(42 : i64) : !llvm.i64
 
Index: mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
===================================================================
--- mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -1010,7 +1010,7 @@
   return success();
 }
 
-// operation ::= `llvm.mlir.global` linkage `constant`? `@` identifier
+// operation ::= `llvm.mlir.global` linkage? `constant`? `@` identifier
 //               `(` attribute? `)` attribute-list? (`:` type)? region?
 //
 // The type can be omitted for string attributes, in which case it will be
@@ -1018,7 +1018,9 @@
 static ParseResult parseGlobalOp(OpAsmParser &parser, OperationState &result) {
   if (failed(parseOptionalLLVMKeyword<Linkage>(parser, result,
                                                getLinkageAttrName())))
-    return parser.emitError(parser.getCurrentLocation(), "expected linkage");
+    result.addAttribute(getLinkageAttrName(),
+                        parser.getBuilder().getI64IntegerAttr(
+                            static_cast<int64_t>(LLVM::Linkage::External)));
 
   if (succeeded(parser.parseOptionalKeyword("constant")))
     result.addAttribute("constant", parser.getBuilder().getUnitAttr());
Index: mlir/docs/Dialects/LLVM.md
===================================================================
--- mlir/docs/Dialects/LLVM.md
+++ mlir/docs/Dialects/LLVM.md
@@ -105,6 +105,14 @@
 If the attribute is not known to LLVM IR, it will be attached as a string
 attribute.
 
+#### Linkage
+
+An LLVM IR dialect function has a linkage attribute derived from LLVM IR
+[linkage types](https://llvm.org/docs/LangRef.html#linkage-types). Linkage is
+specified by the same keyword as in LLVM IR and is located between `llvm.func`
+and the symbol name. If no linkage keyword is present, `external` linkage is
+assumed by default.
+
 ### LLVM IR operations
 
 The following operations are currently supported. The semantics of these
@@ -423,6 +431,21 @@
 }
 ```
 
+Similarly to functions, globals have a linkage attribute. In the custom syntax,
+this attribute is placed between `llvm.mlir.global` and the optional `constant`
+keyword. If the attribute is omitted, `external` linkage is assumed by default.
+
+Examples:
+
+```mlir
+// A constant with internal linkage will not participate in linking.
+llvm.mlir.global internal constant @cst(42 : i32) : !llvm.i32
+
+// By default, "external" linkage is assumed and the global participates in
+// symbol resolution at link-time.
+llvm.mlir.global @glob(0 : f32) : !llvm.float
+```
+
 #### `llvm.mlir.null`
 
 Unlike LLVM IR, MLIR does not have first-class null pointers. They must be


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78096.257631.patch
Type: text/x-patch
Size: 3289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200415/2c4ded07/attachment.bin>


More information about the llvm-commits mailing list