|
8 | 8 |
|
9 | 9 | #include "mlir/Parser/Parser.h"
|
10 | 10 | #include "mlir/AsmParser/AsmParser.h"
|
| 11 | +#include "mlir/AsmParser/AsmParserState.h" |
| 12 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 13 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
11 | 14 | #include "mlir/IR/BuiltinOps.h"
|
12 | 15 | #include "mlir/IR/Verifier.h"
|
| 16 | +#include "llvm/Support/SourceMgr.h" |
13 | 17 |
|
14 | 18 | #include "gmock/gmock.h"
|
15 | 19 |
|
@@ -101,4 +105,81 @@ TEST(MLIRParser, ParseAttr) {
|
101 | 105 | EXPECT_EQ(attr, b.getI64IntegerAttr(9));
|
102 | 106 | }
|
103 | 107 | }
|
| 108 | + |
| 109 | +TEST(MLIRParser, AsmParserLocations) { |
| 110 | + std::string moduleStr = R"mlir( |
| 111 | +func.func @foo() -> !llvm.array<2 x f32> { |
| 112 | + %0 = llvm.mlir.undef : !llvm.array<2 x f32> |
| 113 | + func.return %0 : !llvm.array<2 x f32> |
| 114 | +} |
| 115 | + )mlir"; |
| 116 | + |
| 117 | + DialectRegistry registry; |
| 118 | + registry.insert<func::FuncDialect, LLVM::LLVMDialect>(); |
| 119 | + MLIRContext context(registry); |
| 120 | + |
| 121 | + auto memBuffer = |
| 122 | + llvm::MemoryBuffer::getMemBuffer(moduleStr, "AsmParserTest.mlir", |
| 123 | + /*RequiresNullTerminator=*/false); |
| 124 | + ASSERT_TRUE(memBuffer); |
| 125 | + |
| 126 | + llvm::SourceMgr sourceMgr; |
| 127 | + sourceMgr.AddNewSourceBuffer(std::move(memBuffer), llvm::SMLoc()); |
| 128 | + |
| 129 | + Block block; |
| 130 | + AsmParserState parseState; |
| 131 | + const LogicalResult parseResult = |
| 132 | + parseAsmSourceFile(sourceMgr, &block, &context, &parseState); |
| 133 | + ASSERT_TRUE(parseResult.succeeded()); |
| 134 | + |
| 135 | + auto funcOp = *block.getOps<func::FuncOp>().begin(); |
| 136 | + const AsmParserState::OperationDefinition *funcOpDefinition = |
| 137 | + parseState.getOpDef(funcOp); |
| 138 | + ASSERT_TRUE(funcOpDefinition); |
| 139 | + |
| 140 | + const std::pair expectedStartFunc{2u, 1u}; |
| 141 | + const std::pair expectedEndFunc{2u, 10u}; |
| 142 | + const std::pair expectedScopeEndFunc{5u, 2u}; |
| 143 | + ASSERT_EQ(sourceMgr.getLineAndColumn(funcOpDefinition->loc.Start), |
| 144 | + expectedStartFunc); |
| 145 | + ASSERT_EQ(sourceMgr.getLineAndColumn(funcOpDefinition->loc.End), |
| 146 | + expectedEndFunc); |
| 147 | + ASSERT_EQ(funcOpDefinition->loc.Start, funcOpDefinition->scopeLoc.Start); |
| 148 | + ASSERT_EQ(sourceMgr.getLineAndColumn(funcOpDefinition->scopeLoc.End), |
| 149 | + expectedScopeEndFunc); |
| 150 | + |
| 151 | + auto llvmUndef = *funcOp.getOps<LLVM::UndefOp>().begin(); |
| 152 | + const AsmParserState::OperationDefinition *llvmUndefDefinition = |
| 153 | + parseState.getOpDef(llvmUndef); |
| 154 | + ASSERT_TRUE(llvmUndefDefinition); |
| 155 | + |
| 156 | + const std::pair expectedStartUndef{3u, 8u}; |
| 157 | + const std::pair expectedEndUndef{3u, 23u}; |
| 158 | + const std::pair expectedScopeEndUndef{3u, 46u}; |
| 159 | + ASSERT_EQ(sourceMgr.getLineAndColumn(llvmUndefDefinition->loc.Start), |
| 160 | + expectedStartUndef); |
| 161 | + ASSERT_EQ(sourceMgr.getLineAndColumn(llvmUndefDefinition->loc.End), |
| 162 | + expectedEndUndef); |
| 163 | + ASSERT_EQ(llvmUndefDefinition->loc.Start, |
| 164 | + llvmUndefDefinition->scopeLoc.Start); |
| 165 | + ASSERT_EQ(sourceMgr.getLineAndColumn(llvmUndefDefinition->scopeLoc.End), |
| 166 | + expectedScopeEndUndef); |
| 167 | + |
| 168 | + auto funcReturn = *funcOp.getOps<func::ReturnOp>().begin(); |
| 169 | + const AsmParserState::OperationDefinition *funcReturnDefinition = |
| 170 | + parseState.getOpDef(funcReturn); |
| 171 | + ASSERT_TRUE(funcReturnDefinition); |
| 172 | + |
| 173 | + const std::pair expectedStartReturn{4u, 3u}; |
| 174 | + const std::pair expectedEndReturn{4u, 14u}; |
| 175 | + const std::pair expectedScopeEndReturn{4u, 40u}; |
| 176 | + ASSERT_EQ(sourceMgr.getLineAndColumn(funcReturnDefinition->loc.Start), |
| 177 | + expectedStartReturn); |
| 178 | + ASSERT_EQ(sourceMgr.getLineAndColumn(funcReturnDefinition->loc.End), |
| 179 | + expectedEndReturn); |
| 180 | + ASSERT_EQ(funcReturnDefinition->loc.Start, |
| 181 | + funcReturnDefinition->scopeLoc.Start); |
| 182 | + ASSERT_EQ(sourceMgr.getLineAndColumn(funcReturnDefinition->scopeLoc.End), |
| 183 | + expectedScopeEndReturn); |
| 184 | +} |
104 | 185 | } // namespace
|
0 commit comments