Skip to content

Commit fd1103a

Browse files
committed
Addressing comments
1 parent defb45c commit fd1103a

File tree

3 files changed

+63
-38
lines changed

3 files changed

+63
-38
lines changed

mlir/include/mlir/IR/BuiltinLocationAttributes.td

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def FileLineColRange : Builtin_LocationAttr<"FileLineColRange"> {
9090
```
9191
}];
9292

93-
// Note: this only shows the parameters for which accessors are generated. The
94-
// locations are only set in storage.
95-
let parameters = (ins "StringAttr":$filename);
93+
let parameters = (ins "StringAttr":$filename,
94+
"unsigned":$start_line, "unsigned":$start_column,
95+
"unsigned":$end_line, "unsigned":$end_column);
9696
let builders = [
9797
AttrBuilderWithInferredContext<(ins "StringAttr":$filename), [{
9898
return $_get(filename.getContext(), filename, ArrayRef<unsigned>{});
@@ -142,12 +142,14 @@ def FileLineColRange : Builtin_LocationAttr<"FileLineColRange"> {
142142
];
143143

144144
let extraClassDeclaration = [{
145+
::mlir::StringAttr getFilename() const;
145146
std::optional<unsigned> getStartLine() const;
146147
std::optional<unsigned> getStartColumn() const;
147148
std::optional<unsigned> getEndColumn() const;
148149
std::optional<unsigned> getEndLine() const;
149150
}];
150151
let skipDefaultBuilders = 1;
152+
let genAccessors = 0;
151153
let genStorageClass = 0;
152154
let attrName = "builtin.file_line_range";
153155
}

mlir/lib/IR/BuiltinDialectBytecode.cpp

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ readPotentiallySplatString(DialectBytecodeReader &reader, ShapedType type,
7373
return success();
7474
}
7575

76-
void writePotentiallySplatString(DialectBytecodeWriter &writer,
77-
DenseStringElementsAttr attr) {
76+
static void writePotentiallySplatString(DialectBytecodeWriter &writer,
77+
DenseStringElementsAttr attr) {
7878
bool isSplat = attr.isSplat();
7979
if (isSplat)
8080
return writer.writeOwnedString(attr.getRawStringData().front());
@@ -83,8 +83,9 @@ void writePotentiallySplatString(DialectBytecodeWriter &writer,
8383
writer.writeOwnedString(str);
8484
}
8585

86-
FileLineColRange getFileLineColRange(MLIRContext *context, StringAttr filename,
87-
ArrayRef<uint64_t> lineCols) {
86+
static FileLineColRange getFileLineColRange(MLIRContext *context,
87+
StringAttr filename,
88+
ArrayRef<uint64_t> lineCols) {
8889
switch (lineCols.size()) {
8990
case 0:
9091
return FileLineColRange::get(filename);
@@ -97,34 +98,52 @@ FileLineColRange getFileLineColRange(MLIRContext *context, StringAttr filename,
9798
lineCols[2]);
9899
case 4:
99100
return FileLineColRange::get(filename, lineCols[0], lineCols[1],
100-
lineCols[3], lineCols[2]);
101+
lineCols[2], lineCols[3]);
101102
default:
102103
return {};
103104
}
104105
}
105106

106-
LogicalResult readFileLineColRangeLocs(DialectBytecodeReader &reader,
107+
static LogicalResult readFileLineColRangeLocs(DialectBytecodeReader &reader,
107108
SmallVectorImpl<uint64_t> &lineCols) {
108109
return reader.readList(
109110
lineCols, [&reader](uint64_t &val) { return reader.readVarInt(val); });
110111
}
111112

112-
void writeFileLineColRangeLocs(DialectBytecodeWriter &writer,
113-
FileLineColRange range) {
114-
int count = range.getStartLine().has_value() +
115-
range.getStartColumn().has_value() +
116-
range.getEndLine().has_value() + range.getEndColumn().has_value();
117-
writer.writeVarInt(count);
118-
if (count == 0)
113+
static void writeFileLineColRangeLocs(DialectBytecodeWriter &writer,
114+
FileLineColRange range) {
115+
if (range.getStartLine() == range.getStartColumn() == range.getEndLine() ==
116+
range.getEndColumn() == 0) {
117+
writer.writeVarInt(0);
119118
return;
120-
// Startline here is either known or zero with other trailing offsets.
121-
writer.writeVarInt(range.getStartLine().value_or(0));
122-
if (range.getStartColumn().has_value())
123-
writer.writeVarInt(*range.getStartColumn());
124-
if (range.getEndColumn().has_value())
125-
writer.writeVarInt(*range.getEndColumn());
126-
if (range.getEndLine().has_value())
127-
writer.writeVarInt(*range.getEndLine());
119+
}
120+
if (range.getStartColumn() == 0 &&
121+
range.getStartLine() == range.getEndLine()) {
122+
writer.writeVarInt(1);
123+
writer.writeVarInt(range.getStartLine());
124+
return;
125+
}
126+
// The single file:line:col is handled by other writer, but checked here for
127+
// completeness.
128+
if (range.endColumn() == range.startColumn() &&
129+
range.getStartLine() == range.getEndLine()) {
130+
writer.writeVarInt(2);
131+
writer.writeVarInt(range.getStartLine());
132+
writer.writeVarInt(range.getStartColumn());
133+
return;
134+
}
135+
if (range.getStartLine() == range.getEndLine()) {
136+
writer.writeVarInt(3);
137+
writer.writeVarInt(range.getStartLine());
138+
writer.writeVarInt(range.getStartColumn());
139+
writer.writeVarInt(range.getEndColumn());
140+
return;
141+
}
142+
writer.writeVarInt(4);
143+
writer.writeVarInt(range.getStartLine());
144+
writer.writeVarInt(range.getStartColumn());
145+
writer.writeVarInt(range.getEndLine());
146+
writer.writeVarInt(range.getEndColumn());
128147
}
129148

130149
#include "mlir/IR/BuiltinDialectBytecode.cpp.inc"

mlir/lib/IR/Location.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,25 @@ struct FileLineColRangeAttrStorage final
7878
ArrayRef<unsigned>{std::get<1>(tblgenKey)}.drop_front());
7979
}
8080

81-
std::optional<unsigned> getLineCols(unsigned index) const {
82-
if (size() <= index)
83-
return std::nullopt;
81+
unsigned getLineCols(unsigned index) const {
8482
return getTrailingObjects<unsigned>()[index - 1];
8583
}
8684

87-
std::optional<unsigned> getStartLine() const {
88-
// Only return nullopt if there are no other locations and start line is 0.
89-
if (startLine == 0 && filenameAndTrailing.getInt() == 0)
90-
return std::nullopt;
85+
unsigned getStartLine() const {
9186
return startLine;
9287
}
93-
std::optional<unsigned> getStartColumn() const { return getLineCols(1); }
94-
std::optional<unsigned> getEndColumn() const { return getLineCols(2); }
95-
std::optional<unsigned> getEndLine() const { return getLineCols(3); }
88+
unsigned getStartColumn() const {
89+
if (size() <= 1) return 0;
90+
return getLineCols(1);
91+
}
92+
unsigned getEndColumn() const {
93+
if (size() <= 2) return getStartColumn();
94+
return getLineCols(2);
95+
}
96+
unsigned getEndLine() const {
97+
if (size() <= 3) return getStartLine();
98+
return getLineCols(3);
99+
}
96100

97101
static ::llvm::hash_code hashKey(const KeyTy &tblgenKey) {
98102
return ::llvm::hash_combine(std::get<0>(tblgenKey), std::get<1>(tblgenKey));
@@ -188,16 +192,16 @@ StringAttr FileLineColRange::getFilename() const {
188192
return getImpl()->filenameAndTrailing.getPointer();
189193
}
190194

191-
std::optional<unsigned> FileLineColRange::getStartLine() const {
195+
unsigned FileLineColRange::getStartLine() const {
192196
return getImpl()->getStartLine();
193197
}
194-
std::optional<unsigned> FileLineColRange::getStartColumn() const {
198+
unsigned FileLineColRange::getStartColumn() const {
195199
return getImpl()->getStartColumn();
196200
}
197-
std::optional<unsigned> FileLineColRange::getEndColumn() const {
201+
unsigned FileLineColRange::getEndColumn() const {
198202
return getImpl()->getEndColumn();
199203
}
200-
std::optional<unsigned> FileLineColRange::getEndLine() const {
204+
unsigned FileLineColRange::getEndLine() const {
201205
return getImpl()->getEndLine();
202206
}
203207

0 commit comments

Comments
 (0)