Skip to content

Commit d46c1bc

Browse files
committed
[mlir] load dialects for non-namespaced attrs
The mlir-translate tool calls into the parser without loading registered dependent dialects, and the parser only loads attributes if the fully-namespaced attribute is present in the textual IR. This causes parsing to break when an op has an attribute that prints/parses without the namespaced attribute.
1 parent 5aabbf0 commit d46c1bc

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

mlir/test/Target/LLVMIR/test.mlir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,18 @@ llvm.func @dialect_attr_translation_multi(%a: i64, %b: i64, %c: i64) -> i64 {
4040
// CHECK-DAG: ![[MD_ID_ADD]] = !{!"annotation_from_test: add"}
4141
// CHECK-DAG: ![[MD_ID_MUL]] = !{!"annotation_from_test: mul"}
4242
// CHECK-DAG: ![[MD_ID_RET]] = !{!"annotation_from_test: ret"}
43+
44+
45+
// -----
46+
47+
// This is a regression test for a bug where, during an mlir-translate call the
48+
// parser would only load the dialect if the fully namespaced attribute was
49+
// present in the IR.
50+
#attr = #test.nested_polynomial<<1 + x**2>>
51+
llvm.func @parse_correctly() {
52+
// CHECK: <1 + x**2>
53+
test.containing_int_polynomial_attr #attr
54+
55+
return
56+
}
57+

mlir/test/lib/Dialect/Test/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ mlir_tablegen(TestOpInterfaces.cpp.inc -gen-op-interface-defs)
1616
add_public_tablegen_target(MLIRTestInterfaceIncGen)
1717

1818
set(LLVM_TARGET_DEFINITIONS TestOps.td)
19-
mlir_tablegen(TestAttrDefs.h.inc -gen-attrdef-decls)
20-
mlir_tablegen(TestAttrDefs.cpp.inc -gen-attrdef-defs)
19+
mlir_tablegen(TestAttrDefs.h.inc -gen-attrdef-decls -attrdefs-dialect=test)
20+
mlir_tablegen(TestAttrDefs.cpp.inc -gen-attrdef-defs -attrdefs-dialect=test)
2121
add_public_tablegen_target(MLIRTestAttrDefIncGen)
2222

2323
set(LLVM_TARGET_DEFINITIONS TestTypeDefs.td)
@@ -86,6 +86,7 @@ add_mlir_library(MLIRTestDialect
8686
MLIRLinalgTransforms
8787
MLIRLLVMDialect
8888
MLIRPass
89+
MLIRPolynomialDialect
8990
MLIRReduce
9091
MLIRTensorDialect
9192
MLIRTransformUtils

mlir/test/lib/Dialect/Test/TestAttrDefs.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// To get the test dialect definition.
1717
include "TestDialect.td"
1818
include "TestEnumDefs.td"
19+
include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.td"
1920
include "mlir/Dialect/Utils/StructuredOpsUtils.td"
2021
include "mlir/IR/AttrTypeBase.td"
2122
include "mlir/IR/BuiltinAttributeInterfaces.td"
@@ -351,4 +352,12 @@ def TestCustomFloatAttr : Test_Attr<"TestCustomFloat"> {
351352
}];
352353
}
353354

355+
def NestedPolynomialAttr : Test_Attr<"NestedPolynomialAttr"> {
356+
let mnemonic = "nested_polynomial";
357+
let parameters = (ins Polynomial_IntPolynomialAttr:$poly);
358+
let assemblyFormat = [{
359+
`<` $poly `>`
360+
}];
361+
}
362+
354363
#endif // TEST_ATTRDEFS

mlir/test/lib/Dialect/Test/TestAttributes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <tuple>
1818

1919
#include "TestTraits.h"
20+
#include "mlir/Dialect/Polynomial/IR/PolynomialAttributes.h"
2021
#include "mlir/Dialect/Utils/StructuredOpsUtils.h"
2122
#include "mlir/IR/Attributes.h"
2223
#include "mlir/IR/Diagnostics.h"

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ def FloatElementsAttrOp : TEST_Op<"float_elements_attr"> {
232232
);
233233
}
234234

235+
def ContainingIntPolynomialAttrOp : TEST_Op<"containing_int_polynomial_attr"> {
236+
let arguments = (ins NestedPolynomialAttr:$attr);
237+
let assemblyFormat = "$attr attr-dict";
238+
}
239+
235240
// A pattern that updates dense<[3.0, 4.0]> to dense<[5.0, 6.0]>.
236241
// This tests both matching and generating float elements attributes.
237242
def UpdateFloatElementsAttr : Pat<
@@ -2204,7 +2209,7 @@ def ForwardBufferOp : TEST_Op<"forward_buffer", [Pure]> {
22042209
def ReifyBoundOp : TEST_Op<"reify_bound", [Pure]> {
22052210
let description = [{
22062211
Reify a bound for the given index-typed value or dimension size of a shaped
2207-
value. "LB", "EQ" and "UB" bounds are supported. If `scalable` is set,
2212+
value. "LB", "EQ" and "UB" bounds are supported. If `scalable` is set,
22082213
`vscale_min` and `vscale_max` must be provided, which allows computing
22092214
a bound in terms of "vector.vscale" for a given range of vscale.
22102215
}];

mlir/test/lib/Dialect/Test/TestToLLVMIRTranslation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "mlir/IR/Builders.h"
1616
#include "mlir/IR/BuiltinAttributes.h"
1717
#include "mlir/IR/BuiltinOps.h"
18+
#include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h"
1819
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
1920
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
2021
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
@@ -127,6 +128,7 @@ void registerTestToLLVMIR() {
127128
},
128129
[](DialectRegistry &registry) {
129130
registry.insert<test::TestDialect>();
131+
registry.insert<polynomial::PolynomialDialect>();
130132
registerBuiltinDialectTranslation(registry);
131133
registerLLVMDialectTranslation(registry);
132134
registry.addExtension(

0 commit comments

Comments
 (0)