Skip to content

Commit ea0aee4

Browse files
committed
Address code review comments
1 parent 4ae9c42 commit ea0aee4

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
4444
return false;
4545
}
4646

47+
// Return true if this is the null value
4748
bool isNullValue(mlir::Attribute attr) const {
4849
if (mlir::isa<cir::ZeroAttr>(attr))
4950
return true;

clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ static mlir::Attribute
169169
emitArrayConstant(CIRGenModule &cgm, mlir::Type desiredType,
170170
mlir::Type commonElementType, unsigned arrayBound,
171171
SmallVectorImpl<mlir::TypedAttr> &elements,
172-
mlir::TypedAttr filter) {
173-
const auto &builder = cgm.getBuilder();
172+
mlir::TypedAttr filler) {
173+
const CIRGenBuilderTy &builder = cgm.getBuilder();
174174

175175
unsigned nonzeroLength = arrayBound;
176-
if (elements.size() < nonzeroLength && builder.isNullValue(filter))
176+
if (elements.size() < nonzeroLength && builder.isNullValue(filler))
177177
nonzeroLength = elements.size();
178178

179179
if (nonzeroLength == elements.size()) {
@@ -186,13 +186,15 @@ emitArrayConstant(CIRGenModule &cgm, mlir::Type desiredType,
186186
return cir::ZeroAttr::get(builder.getContext(), desiredType);
187187

188188
const unsigned trailingZeroes = arrayBound - nonzeroLength;
189+
190+
// Add a zeroinitializer array filler if we have lots of trailing zeroes.
189191
if (trailingZeroes >= 8) {
190-
if (elements.size() < nonzeroLength)
191-
cgm.errorNYI("missing initializer for non-zero element");
192+
assert(elements.size() >= nonzeroLength &&
193+
"missing initializer for non-zero element");
192194
} else if (elements.size() != arrayBound) {
193-
elements.resize(arrayBound, filter);
195+
elements.resize(arrayBound, filler);
194196

195-
if (filter.getType() != commonElementType)
197+
if (filler.getType() != commonElementType)
196198
cgm.errorNYI(
197199
"array filter type should always be the same as element type");
198200
}
@@ -327,16 +329,16 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &value,
327329
const unsigned numElements = value.getArraySize();
328330
const unsigned numInitElts = value.getArrayInitializedElts();
329331

330-
mlir::Attribute filter;
332+
mlir::Attribute filler;
331333
if (value.hasArrayFiller()) {
332-
filter =
333-
tryEmitPrivate(value.getArrayFiller(), arrayTy->getElementType());
334-
if (!filter)
334+
filler =
335+
tryEmitPrivate(value.getArrayFiller(), arrayElementTy);
336+
if (!filler)
335337
return {};
336338
}
337339

338340
SmallVector<mlir::TypedAttr, 16> elements;
339-
if (filter && builder.isNullValue(filter))
341+
if (filler && builder.isNullValue(filler))
340342
elements.reserve(numInitElts + 1);
341343
else
342344
elements.reserve(numInitElts);
@@ -361,14 +363,14 @@ mlir::Attribute ConstantEmitter::tryEmitPrivate(const APValue &value,
361363
elements.push_back(elementTyped);
362364
}
363365

364-
mlir::TypedAttr typedFilter =
365-
llvm::dyn_cast_or_null<mlir::TypedAttr>(filter);
366-
if (filter && !typedFilter)
367-
cgm.errorNYI("array filter should always be typed");
366+
mlir::TypedAttr typedFiller =
367+
llvm::cast_or_null<mlir::TypedAttr>(filler);
368+
if (filler && !typedFiller)
369+
cgm.errorNYI("array filler should always be typed");
368370

369371
mlir::Type desiredType = cgm.convertType(destType);
370372
return emitArrayConstant(cgm, desiredType, commonElementType, numElements,
371-
elements, typedFilter);
373+
elements, typedFiller);
372374
}
373375
case APValue::Vector: {
374376
cgm.errorNYI("ConstExprEmitter::tryEmitPrivate vector");

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ ConstArrayAttr::verify(function_ref<::mlir::InFlightDiagnostic()> emitError,
201201
if (!(mlir::isa<ArrayAttr>(elts) || mlir::isa<StringAttr>(elts)))
202202
return emitError() << "constant array expects ArrayAttr or StringAttr";
203203

204-
if (StringAttr strAttr = mlir::dyn_cast<StringAttr>(elts)) {
205-
ArrayType arrayTy = mlir::cast<ArrayType>(type);
206-
IntType intTy = mlir::dyn_cast<IntType>(arrayTy.getEltType());
204+
if (auto strAttr = mlir::dyn_cast<StringAttr>(elts)) {
205+
const auto arrayTy = mlir::cast<ArrayType>(type);
206+
const auto intTy = mlir::dyn_cast<IntType>(arrayTy.getEltType());
207207

208208
// TODO: add CIR type for char.
209209
if (!intTy || intTy.getWidth() != 8) {
@@ -215,8 +215,8 @@ ConstArrayAttr::verify(function_ref<::mlir::InFlightDiagnostic()> emitError,
215215
}
216216

217217
assert(mlir::isa<ArrayAttr>(elts));
218-
ArrayAttr arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
219-
ArrayType arrayTy = mlir::cast<ArrayType>(type);
218+
const auto arrayAttr = mlir::cast<mlir::ArrayAttr>(elts);
219+
const auto arrayTy = mlir::cast<ArrayType>(type);
220220

221221
// Make sure both number of elements and subelement types match type.
222222
if (arrayTy.getSize() != arrayAttr.size() + trailingZerosNum)
@@ -225,10 +225,9 @@ ConstArrayAttr::verify(function_ref<::mlir::InFlightDiagnostic()> emitError,
225225
}
226226

227227
Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
228-
::mlir::FailureOr<Type> resultTy;
229-
::mlir::FailureOr<Attribute> resultVal;
230-
::llvm::SMLoc loc = parser.getCurrentLocation();
231-
(void)loc;
228+
mlir::FailureOr<Type> resultTy;
229+
mlir::FailureOr<Attribute> resultVal;
230+
232231
// Parse literal '<'
233232
if (parser.parseLess())
234233
return {};
@@ -244,7 +243,7 @@ Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
244243
}
245244

246245
// ArrayAttrrs have per-element type, not the type of the array...
247-
if (mlir::dyn_cast<ArrayAttr>(*resultVal)) {
246+
if (mlir::isa<ArrayAttr>(*resultVal)) {
248247
// Array has implicit type: infer from const array type.
249248
if (parser.parseOptionalColon().failed()) {
250249
resultTy = type;
@@ -259,7 +258,6 @@ Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
259258
}
260259
}
261260
} else {
262-
assert(mlir::isa<TypedAttr>(*resultVal) && "IDK");
263261
auto ta = mlir::cast<TypedAttr>(*resultVal);
264262
resultTy = ta.getType();
265263
if (mlir::isa<mlir::NoneType>(*resultTy)) {
@@ -269,11 +267,11 @@ Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
269267
}
270268
}
271269

272-
auto zeros = 0;
270+
unsigned zeros = 0;
273271
if (parser.parseOptionalComma().succeeded()) {
274272
if (parser.parseOptionalKeyword("trailing_zeros").succeeded()) {
275-
auto typeSize = mlir::cast<cir::ArrayType>(resultTy.value()).getSize();
276-
auto elts = resultVal.value();
273+
unsigned typeSize = mlir::cast<cir::ArrayType>(resultTy.value()).getSize();
274+
mlir::Attribute elts = resultVal.value();
277275
if (auto str = mlir::dyn_cast<mlir::StringAttr>(elts))
278276
zeros = typeSize - str.size();
279277
else
@@ -288,7 +286,7 @@ Attribute ConstArrayAttr::parse(AsmParser &parser, Type type) {
288286
return {};
289287

290288
return parser.getChecked<ConstArrayAttr>(
291-
loc, parser.getContext(), resultTy.value(), resultVal.value(), zeros);
289+
parser.getCurrentLocation(), parser.getContext(), resultTy.value(), resultVal.value(), zeros);
292290
}
293291

294292
void ConstArrayAttr::print(AsmPrinter &printer) const {

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ mlir::Value lowerCirAttrAsValue(mlir::Operation *parentOp,
189189
mlir::ConversionPatternRewriter &rewriter,
190190
const mlir::TypeConverter *converter) {
191191
CIRAttrToValue valueConverter(parentOp, rewriter, converter);
192-
auto value = valueConverter.visit(attr);
192+
mlir::Value value = valueConverter.visit(attr);
193193
if (!value)
194194
llvm_unreachable("unhandled attribute type");
195195
return value;
@@ -242,8 +242,7 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstArrayAttr attr) {
242242
if (auto arrayAttr = mlir::dyn_cast<mlir::ArrayAttr>(attr.getElts())) {
243243
for (auto [idx, elt] : llvm::enumerate(arrayAttr)) {
244244
mlir::DataLayout dataLayout(parentOp->getParentOfType<mlir::ModuleOp>());
245-
mlir::Value init =
246-
emitCirAttrToMemory(parentOp, elt, rewriter, converter, dataLayout);
245+
mlir::Value init = visit(elt);
247246
result =
248247
rewriter.create<mlir::LLVM::InsertValueOp>(loc, result, init, idx);
249248
}

0 commit comments

Comments
 (0)