Skip to content

Commit bc98ede

Browse files
committed
Add test
1 parent 5cc096f commit bc98ede

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics
2+
3+
// Test basic symbol verification using discardable attribute.
4+
module {
5+
func.func @existing_symbol() { return }
6+
7+
func.func @test() attributes {symbol_ref = #test.symbol_ref_attr<@existing_symbol>} { return }
8+
}
9+
10+
// -----
11+
12+
// Test invalid symbol reference, symbol does not exist.
13+
module {
14+
// expected-error@+1 {{TestSymbolRefAttr::verifySymbolUses: '@non_existent_symbol' does not reference a valid symbol}}
15+
func.func @test() attributes {symbol_ref = #test.symbol_ref_attr<@non_existent_symbol>} { return }
16+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ include "mlir/IR/AttrTypeBase.td"
2222
include "mlir/IR/BuiltinAttributeInterfaces.td"
2323
include "mlir/IR/EnumAttr.td"
2424
include "mlir/IR/OpAsmInterface.td"
25+
include "mlir/IR/SymbolInterfaces.td"
2526
include "mlir/IR/TensorEncoding.td"
2627

2728
// All of the attributes will extend this class.
@@ -456,4 +457,17 @@ def TestMemRefLayoutAttr : Test_Attr<"TestMemRefLayout",
456457
let assemblyFormat = "`<` $dummy `>`";
457458
}
458459

460+
// Test attribute that implements SymbolUserAttrInterface.
461+
def TestSymbolRefAttr : Test_Attr<"TestSymbolRef",
462+
[DeclareAttrInterfaceMethods<SymbolUserAttrInterface>]> {
463+
let mnemonic = "symbol_ref_attr";
464+
let summary = "Test attribute that references a symbol";
465+
let description = [{
466+
This attribute holds a reference to a symbol and implements
467+
SymbolUserAttrInterface to verify that the referenced symbol exists.
468+
}];
469+
let parameters = (ins "::mlir::FlatSymbolRefAttr":$symbol);
470+
let assemblyFormat = "`<` $symbol `>`";
471+
}
472+
459473
#endif // TEST_ATTRDEFS

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,25 @@ LogicalResult TestCopyCountAttr::verify(
223223
return success();
224224
}
225225

226+
//===----------------------------------------------------------------------===//
227+
// TestSymbolRefAttr
228+
//===----------------------------------------------------------------------===//
229+
230+
LogicalResult
231+
TestSymbolRefAttr::verifySymbolUses(Operation *op,
232+
SymbolTableCollection &symbolTable) const {
233+
// Verify that the referenced symbol exists
234+
if (!symbolTable.lookupNearestSymbolFrom<SymbolOpInterface>(op, getSymbol()))
235+
return op->emitOpError()
236+
<< "TestSymbolRefAttr::verifySymbolUses: '" << getSymbol()
237+
<< "' does not reference a valid symbol";
238+
return success();
239+
}
240+
241+
//===----------------------------------------------------------------------===//
242+
// Generated Attribute Definitions
243+
//===----------------------------------------------------------------------===//
244+
226245
//===----------------------------------------------------------------------===//
227246
// CopyCountAttr Implementation
228247
//===----------------------------------------------------------------------===//

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
2121
#include "mlir/Dialect/Utils/StructuredOpsUtils.h"
2222
#include "mlir/IR/Attributes.h"
23-
#include "mlir/IR/Diagnostics.h"
23+
#include "mlir/IR/BuiltinAttributes.h"
2424
#include "mlir/IR/Dialect.h"
25-
#include "mlir/IR/DialectImplementation.h"
2625
#include "mlir/IR/DialectResourceBlobManager.h"
26+
#include "mlir/IR/OpImplementation.h"
27+
#include "mlir/IR/SymbolTable.h"
2728
#include "mlir/IR/TensorEncoding.h"
2829

2930
// generated files require above includes to come first

0 commit comments

Comments
 (0)