@@ -698,17 +698,25 @@ lowerCirAttrAsValue(mlir::Operation *parentOp, cir::GlobalViewAttr globalAttr,
698698 indices, true );
699699 }
700700
701- auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType ());
702- assert (ptrTy && " Expecting pointer type in GlobalViewAttr" );
703- auto llvmEltTy =
704- convertTypeForMemory (*converter, dataLayout, ptrTy.getPointee ());
701+ if (auto intTy = mlir::dyn_cast<cir::IntType>(globalAttr.getType ())) {
702+ auto llvmDstTy = converter->convertType (globalAttr.getType ());
703+ return rewriter.create <mlir::LLVM::PtrToIntOp>(parentOp->getLoc (),
704+ llvmDstTy, addrOp);
705+ }
706+
707+ if (auto ptrTy = mlir::dyn_cast<cir::PointerType>(globalAttr.getType ())) {
708+ auto llvmEltTy =
709+ convertTypeForMemory (*converter, dataLayout, ptrTy.getPointee ());
705710
706- if (llvmEltTy == sourceType)
707- return addrOp;
711+ if (llvmEltTy == sourceType)
712+ return addrOp;
708713
709- auto llvmDstTy = converter->convertType (globalAttr.getType ());
710- return rewriter.create <mlir::LLVM::BitcastOp>(parentOp->getLoc (), llvmDstTy,
711- addrOp);
714+ auto llvmDstTy = converter->convertType (globalAttr.getType ());
715+ return rewriter.create <mlir::LLVM::BitcastOp>(parentOp->getLoc (), llvmDstTy,
716+ addrOp);
717+ }
718+
719+ llvm_unreachable (" Expecting pointer or integer type for GlobalViewAttr" );
712720}
713721
714722// / Switches on the type of attribute and calls the appropriate conversion.
@@ -1681,6 +1689,14 @@ mlir::LogicalResult CIRToLLVMConstantOpLowering::matchAndRewrite(
16811689 attr = rewriter.getIntegerAttr (typeConverter->convertType (op.getType ()),
16821690 value);
16831691 } else if (mlir::isa<cir::IntType>(op.getType ())) {
1692+ // Lower GlobalAddrAttr to llvm.mlir.addressof + llvm.mlir.ptrtoint
1693+ if (auto ga = mlir::dyn_cast<cir::GlobalViewAttr>(op.getValue ())) {
1694+ auto newOp =
1695+ lowerCirAttrAsValue (op, ga, rewriter, getTypeConverter (), dataLayout);
1696+ rewriter.replaceOp (op, newOp);
1697+ return mlir::success ();
1698+ }
1699+
16841700 attr = rewriter.getIntegerAttr (
16851701 typeConverter->convertType (op.getType ()),
16861702 mlir::cast<cir::IntAttr>(op.getValue ()).getValue ());
0 commit comments