@@ -62,42 +62,35 @@ namespace fir {
62
62
63
63
// TODO: This should really be recovered from the specified target.
64
64
static constexpr unsigned defaultAlign = 8 ;
65
+ static constexpr unsigned defaultAddressSpace = 0u ;
65
66
66
67
// / `fir.box` attribute values as defined for CFI_attribute_t in
67
68
// / flang/ISO_Fortran_binding.h.
68
69
static constexpr unsigned kAttrPointer = CFI_attribute_pointer;
69
70
static constexpr unsigned kAttrAllocatable = CFI_attribute_allocatable;
70
71
71
- static inline unsigned getAllocaAddressSpace (mlir::ModuleOp module) {
72
- if (mlir::Attribute addrSpace =
73
- mlir::DataLayout (module).getAllocaMemorySpace ())
74
- return addrSpace.cast <mlir::IntegerAttr>().getUInt ();
75
-
76
- return 0u ;
77
- }
78
-
79
- static inline unsigned getProgramAddressSpace (mlir::ModuleOp module) {
80
- if (mlir::Attribute addrSpace =
81
- mlir::DataLayout (module).getProgramMemorySpace ())
82
- return addrSpace.cast <mlir::IntegerAttr>().getUInt ();
83
-
84
- return 0u ;
85
- }
86
-
87
72
static inline unsigned
88
73
getAllocaAddressSpace (mlir::ConversionPatternRewriter &rewriter) {
89
74
mlir::Operation *parentOp = rewriter.getInsertionBlock ()->getParentOp ();
90
- return parentOp ? ::getAllocaAddressSpace (
91
- parentOp->getParentOfType <mlir::ModuleOp>())
92
- : 0u ;
75
+ assert (parentOp != nullptr &&
76
+ " expected insertion block to have parent operation" );
77
+ if (auto module = parentOp->getParentOfType <mlir::ModuleOp>())
78
+ if (mlir::Attribute addrSpace =
79
+ mlir::DataLayout (module).getAllocaMemorySpace ())
80
+ return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt ();
81
+ return defaultAddressSpace;
93
82
}
94
83
95
84
static inline unsigned
96
85
getProgramAddressSpace (mlir::ConversionPatternRewriter &rewriter) {
97
86
mlir::Operation *parentOp = rewriter.getInsertionBlock ()->getParentOp ();
98
- return parentOp ? ::getProgramAddressSpace (
99
- parentOp->getParentOfType <mlir::ModuleOp>())
100
- : 0u ;
87
+ assert (parentOp != nullptr &&
88
+ " expected insertion block to have parent operation" );
89
+ if (auto module = parentOp->getParentOfType <mlir::ModuleOp>())
90
+ if (mlir::Attribute addrSpace =
91
+ mlir::DataLayout (module).getProgramMemorySpace ())
92
+ return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt ();
93
+ return defaultAddressSpace;
101
94
}
102
95
103
96
static inline mlir::Type getLlvmPtrType (mlir::MLIRContext *context,
@@ -402,11 +395,15 @@ class FIROpConversion : public mlir::ConvertOpToLLVMPattern<FromOp> {
402
395
return getBlockForAllocaInsert (op->getParentOp ());
403
396
}
404
397
405
- // Generate an alloca of size 1 for an object of type \p llvmObjectTy.
406
- mlir::Value
407
- genAllocaWithType (mlir::Location loc, mlir::Type llvmObjectTy,
408
- unsigned alignment,
409
- mlir::ConversionPatternRewriter &rewriter) const {
398
+ // Generate an alloca of size 1 for an object of type \p llvmObjectTy in the
399
+ // allocation address space provided for the architecture in the DataLayout
400
+ // specification. If the address space is different from the devices
401
+ // program address space we perform a cast. In the case of most architectures
402
+ // the program and allocation address space will be the default of 0 and no
403
+ // cast will be emitted.
404
+ mlir::Value genAllocaAndAddrCastWithType (
405
+ mlir::Location loc, mlir::Type llvmObjectTy, unsigned alignment,
406
+ mlir::ConversionPatternRewriter &rewriter) const {
410
407
auto thisPt = rewriter.saveInsertionPoint ();
411
408
mlir::Operation *parentOp = rewriter.getInsertionBlock ()->getParentOp ();
412
409
mlir::Block *insertBlock = getBlockForAllocaInsert (parentOp);
@@ -1753,8 +1750,8 @@ struct EmboxCommonConversion : public FIROpConversion<OP> {
1753
1750
if (isInGlobalOp (rewriter))
1754
1751
return boxValue;
1755
1752
mlir::Type llvmBoxTy = boxValue.getType ();
1756
- auto alloca =
1757
- this -> genAllocaWithType (loc, llvmBoxTy, defaultAlign, rewriter);
1753
+ auto alloca = this -> genAllocaAndAddrCastWithType (loc, llvmBoxTy,
1754
+ defaultAlign, rewriter);
1758
1755
auto storeOp = rewriter.create <mlir::LLVM::StoreOp>(loc, boxValue, alloca );
1759
1756
this ->attachTBAATag (storeOp, boxTy, boxTy, nullptr );
1760
1757
return alloca ;
@@ -3172,7 +3169,7 @@ struct LoadOpConversion : public FIROpConversion<fir::LoadOp> {
3172
3169
else
3173
3170
attachTBAATag (boxValue, boxTy, boxTy, nullptr );
3174
3171
auto newBoxStorage =
3175
- genAllocaWithType (loc, llvmLoadTy, defaultAlign, rewriter);
3172
+ genAllocaAndAddrCastWithType (loc, llvmLoadTy, defaultAlign, rewriter);
3176
3173
auto storeOp =
3177
3174
rewriter.create <mlir::LLVM::StoreOp>(loc, boxValue, newBoxStorage);
3178
3175
attachTBAATag (storeOp, boxTy, boxTy, nullptr );
0 commit comments