|
68 | 68 | #include <optional>
|
69 | 69 | #include <set>
|
70 | 70 |
|
| 71 | +#include "LowerModule.h" |
| 72 | + |
71 | 73 | using namespace cir;
|
72 | 74 | using namespace llvm;
|
73 | 75 |
|
@@ -3583,24 +3585,44 @@ void populateCIRToLLVMConversionPatterns(mlir::RewritePatternSet &patterns,
|
3583 | 3585 | }
|
3584 | 3586 |
|
3585 | 3587 | namespace {
|
| 3588 | + |
| 3589 | +using IRASMap = mlir::cir::AddressSpaceAttr::MapTy; |
| 3590 | + |
| 3591 | +/// Returns the CIR address space map when available. Returns nullptr if the |
| 3592 | +/// triple is not present in the module. |
| 3593 | +IRASMap const *getIRAddrSpaceMap(mlir::ModuleOp module) { |
| 3594 | + // If the triple is not present, e.g. CIR modules parsed from text, we |
| 3595 | + // cannot init LowerModule properly. |
| 3596 | + assert(!::cir::MissingFeatures::makeTripleAlwaysPresent()); |
| 3597 | + // Here we will use a default AS map to ignore all AS stuff. |
| 3598 | + if (!module->hasAttr("cir.triple")) |
| 3599 | + return nullptr; |
| 3600 | + mlir::PatternRewriter rewriter{module->getContext()}; |
| 3601 | + auto lowerModule = mlir::cir::createLowerModule(module, rewriter); |
| 3602 | + // All CIR AS maps have static lifetime, it's safe to ref it |
| 3603 | + return &lowerModule.getTargetLoweringInfo().getCIRAddrSpaceMap(); |
| 3604 | +}; |
| 3605 | + |
3586 | 3606 | void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
|
3587 |
| - mlir::DataLayout &dataLayout) { |
3588 |
| - converter.addConversion([&](mlir::cir::PointerType type) -> mlir::Type { |
| 3607 | + mlir::DataLayout &dataLayout, |
| 3608 | + IRASMap const *cirASMap) { |
| 3609 | + converter.addConversion([&, cirASMap]( |
| 3610 | + mlir::cir::PointerType type) -> mlir::Type { |
3589 | 3611 | // Drop pointee type since LLVM dialect only allows opaque pointers.
|
3590 | 3612 |
|
3591 | 3613 | auto addrSpace =
|
3592 | 3614 | mlir::cast_if_present<mlir::cir::AddressSpaceAttr>(type.getAddrSpace());
|
3593 |
| - // null addrspace attribute indicates the default addrspace |
| 3615 | + // Null addrspace attribute indicates the default addrspace |
3594 | 3616 | if (!addrSpace)
|
3595 | 3617 | return mlir::LLVM::LLVMPointerType::get(type.getContext());
|
3596 | 3618 |
|
3597 |
| - // TODO(cir): Query the target-specific address space map to lower other ASs |
3598 |
| - // like `opencl_private`. |
3599 |
| - assert(!MissingFeatures::targetLoweringInfoAddressSpaceMap()); |
3600 |
| - assert(addrSpace.isTarget() && "NYI"); |
| 3619 | + assert(cirASMap && "CIR AS map is not available"); |
| 3620 | + // Pass through target addrspace and map CIR addrspace to LLVM addrspace. |
| 3621 | + unsigned targetAS = addrSpace.isTarget() |
| 3622 | + ? addrSpace.getTargetValue() |
| 3623 | + : (*cirASMap)[addrSpace.getValue()]; |
3601 | 3624 |
|
3602 |
| - return mlir::LLVM::LLVMPointerType::get(type.getContext(), |
3603 |
| - addrSpace.getTargetValue()); |
| 3625 | + return mlir::LLVM::LLVMPointerType::get(type.getContext(), targetAS); |
3604 | 3626 | });
|
3605 | 3627 | converter.addConversion([&](mlir::cir::DataMemberType type) -> mlir::Type {
|
3606 | 3628 | return mlir::IntegerType::get(type.getContext(),
|
@@ -3825,7 +3847,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
|
3825 | 3847 | auto module = getOperation();
|
3826 | 3848 | mlir::DataLayout dataLayout(module);
|
3827 | 3849 | mlir::LLVMTypeConverter converter(&getContext());
|
3828 |
| - prepareTypeConverter(converter, dataLayout); |
| 3850 | + prepareTypeConverter(converter, dataLayout, getIRAddrSpaceMap(module)); |
3829 | 3851 |
|
3830 | 3852 | mlir::RewritePatternSet patterns(&getContext());
|
3831 | 3853 |
|
|
0 commit comments