@@ -620,16 +620,24 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr,
620
620
indices, true );
621
621
}
622
622
623
- auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType ());
624
- assert (ptrTy && " Expecting pointer type in GlobalViewAttr" );
625
- auto llvmEltTy = converter->convertType (ptrTy.getPointee ());
623
+ if (auto intTy = mlir::dyn_cast<cir::IntType>(globalAttr.getType ())) {
624
+ auto llvmDstTy = converter->convertType (globalAttr.getType ());
625
+ return rewriter.create <mlir::LLVM::PtrToIntOp>(parentOp->getLoc (),
626
+ llvmDstTy, addrOp);
627
+ }
628
+
629
+ if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType ())) {
630
+ auto llvmEltTy = converter->convertType (ptrTy.getPointee ());
626
631
627
- if (llvmEltTy == sourceType)
628
- return addrOp;
632
+ if (llvmEltTy == sourceType)
633
+ return addrOp;
634
+
635
+ auto llvmDstTy = converter->convertType (globalAttr.getType ());
636
+ return rewriter.create <mlir::LLVM::BitcastOp>(parentOp->getLoc (), llvmDstTy,
637
+ addrOp);
638
+ }
629
639
630
- auto llvmDstTy = converter->convertType (globalAttr.getType ());
631
- return rewriter.create <mlir::LLVM::BitcastOp>(parentOp->getLoc (), llvmDstTy,
632
- addrOp);
640
+ llvm_unreachable (" Expecting pointer or integer type for GlobalViewAttr" );
633
641
}
634
642
635
643
// / Switches on the type of attribute and calls the appropriate conversion.
@@ -1588,6 +1596,13 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
1588
1596
attr = rewriter.getIntegerAttr (typeConverter->convertType (op.getType ()),
1589
1597
value);
1590
1598
} else if (mlir::isa<cir::IntType>(op.getType ())) {
1599
+ // Lower GlobalAddrAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
1600
+ if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue ())) {
1601
+ auto newOp = lowerCirAttrAsValue (op, ga, rewriter, getTypeConverter ());
1602
+ rewriter.replaceOp (op, newOp);
1603
+ return mlir::success ();
1604
+ }
1605
+
1591
1606
attr = rewriter.getIntegerAttr (
1592
1607
typeConverter->convertType (op.getType ()),
1593
1608
mlir::cast<cir::IntAttr>(op.getValue ()).getValue ());
0 commit comments