@@ -201,8 +201,8 @@ class CIRAttrToValue {
201201 mlir::Value visit (mlir::Attribute attr) {
202202 return llvm::TypeSwitch<mlir::Attribute, mlir::Value>(attr)
203203 .Case <cir::IntAttr, cir::FPAttr, cir::ConstComplexAttr,
204- cir::ConstArrayAttr, cir::ConstVectorAttr , cir::ConstPtrAttr ,
205- cir::GlobalViewAttr, cir::ZeroAttr>(
204+ cir::ConstArrayAttr, cir::ConstRecordAttr , cir::ConstVectorAttr ,
205+ cir::ConstPtrAttr, cir:: GlobalViewAttr, cir::ZeroAttr>(
206206 [&](auto attrT) { return visitCirAttr (attrT); })
207207 .Default ([&](auto attrT) { return mlir::Value (); });
208208 }
@@ -212,6 +212,7 @@ class CIRAttrToValue {
212212 mlir::Value visitCirAttr (cir::ConstComplexAttr complexAttr);
213213 mlir::Value visitCirAttr (cir::ConstPtrAttr ptrAttr);
214214 mlir::Value visitCirAttr (cir::ConstArrayAttr attr);
215+ mlir::Value visitCirAttr (cir::ConstRecordAttr attr);
215216 mlir::Value visitCirAttr (cir::ConstVectorAttr attr);
216217 mlir::Value visitCirAttr (cir::GlobalViewAttr attr);
217218 mlir::Value visitCirAttr (cir::ZeroAttr attr);
@@ -386,6 +387,21 @@ mlir::Value CIRAttrToValue::visitCirAttr(cir::ConstArrayAttr attr) {
386387 return result;
387388}
388389
390+ // / ConstRecord visitor.
391+ mlir::Value CIRAttrToValue::visitCirAttr (cir::ConstRecordAttr constRecord) {
392+ const mlir::Type llvmTy = converter->convertType (constRecord.getType ());
393+ const mlir::Location loc = parentOp->getLoc ();
394+ mlir::Value result = rewriter.create <mlir::LLVM::UndefOp>(loc, llvmTy);
395+
396+ // Iteratively lower each constant element of the record.
397+ for (auto [idx, elt] : llvm::enumerate (constRecord.getMembers ())) {
398+ mlir::Value init = visit (elt);
399+ result = rewriter.create <mlir::LLVM::InsertValueOp>(loc, result, init, idx);
400+ }
401+
402+ return result;
403+ }
404+
389405// / ConstVectorAttr visitor.
390406mlir::Value CIRAttrToValue::visitCirAttr (cir::ConstVectorAttr attr) {
391407 const mlir::Type llvmTy = converter->convertType (attr.getType ());
@@ -1286,6 +1302,11 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
12861302 rewriter.eraseOp (op);
12871303 return mlir::success ();
12881304 }
1305+ } else if (const auto recordAttr =
1306+ mlir::dyn_cast<cir::ConstRecordAttr>(op.getValue ())) {
1307+ auto initVal = lowerCirAttrAsValue (op, recordAttr, rewriter, typeConverter);
1308+ rewriter.replaceOp (op, initVal);
1309+ return mlir::success ();
12891310 } else if (const auto vecTy = mlir::dyn_cast<cir::VectorType>(op.getType ())) {
12901311 rewriter.replaceOp (op, lowerCirAttrAsValue (op, op.getValue (), rewriter,
12911312 getTypeConverter ()));
@@ -1527,9 +1548,9 @@ CIRToLLVMGlobalOpLowering::matchAndRewriteRegionInitializedGlobal(
15271548 cir::GlobalOp op, mlir::Attribute init,
15281549 mlir::ConversionPatternRewriter &rewriter) const {
15291550 // TODO: Generalize this handling when more types are needed here.
1530- assert (
1531- (isa< cir::ConstArrayAttr , cir::ConstVectorAttr , cir::ConstPtrAttr ,
1532- cir::ConstComplexAttr, cir::GlobalViewAttr, cir::ZeroAttr>(init)));
1551+ assert ((isa<cir::ConstArrayAttr, cir::ConstRecordAttr, cir::ConstVectorAttr,
1552+ cir::ConstPtrAttr , cir::ConstComplexAttr , cir::GlobalViewAttr ,
1553+ cir::ZeroAttr>(init)));
15331554
15341555 // TODO(cir): once LLVM's dialect has proper equivalent attributes this
15351556 // should be updated. For now, we use a custom op to initialize globals
@@ -1582,8 +1603,9 @@ mlir::LogicalResult CIRToLLVMGlobalOpLowering::matchAndRewrite(
15821603 return mlir::failure ();
15831604 }
15841605 } else if (mlir::isa<cir::ConstArrayAttr, cir::ConstVectorAttr,
1585- cir::ConstPtrAttr, cir::ConstComplexAttr,
1586- cir::GlobalViewAttr, cir::ZeroAttr>(init.value ())) {
1606+ cir::ConstRecordAttr, cir::ConstPtrAttr,
1607+ cir::ConstComplexAttr, cir::GlobalViewAttr,
1608+ cir::ZeroAttr>(init.value ())) {
15871609 // TODO(cir): once LLVM's dialect has proper equivalent attributes this
15881610 // should be updated. For now, we use a custom op to initialize globals
15891611 // to the appropriate value.
0 commit comments