This PR aims to factor in the allocation address space provided by an architectures data layout when generating the intrinsic instructions, this allows them to be lowered later with the address spaces in tow. This aligns the intrinsic creation with the LLVM IRBuilder's https://github.com/llvm/llvm-project/blob/main/llvm/include/llvm/IR/IRBuilder.h#L1053 This is also necessary for the below example to compile for OpenMP AMD GPU and not ICE the compiler in ISEL as AMD's stackrestore and stacksave are expected to have the appropriate allocation address space for AMD GPU. program main integer(4), allocatable :: test allocate(test) !$omp target map(tofrom:test) do i = 1, 10 test = test + 50 end do !$omp end target deallocate(test) end program The PR also fixes the issue I opened a while ago which hits the same error when compiling for AMDGPU: https://github.com/llvm/llvm-project/issues/82368 Although, you have to have the appropriate GPU LIBC and Fortran offload runtime (both compiled for AMDGPU) added to the linker for the command or it will reach another ISEL error and ICE weirdly. But with the pre-requisites it works fine with this PR.
65 lines
2.0 KiB
C++
65 lines
2.0 KiB
C++
//===- StackReclaim.cpp -- Insert stacksave/stackrestore in region --------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "flang/Common/Fortran.h"
|
|
#include "flang/Optimizer/Dialect/FIRDialect.h"
|
|
#include "flang/Optimizer/Dialect/FIROps.h"
|
|
#include "flang/Optimizer/Transforms/Passes.h"
|
|
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
|
|
#include "mlir/IR/Matchers.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
namespace fir {
|
|
#define GEN_PASS_DEF_STACKRECLAIM
|
|
#include "flang/Optimizer/Transforms/Passes.h.inc"
|
|
} // namespace fir
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
|
|
class StackReclaimPass : public fir::impl::StackReclaimBase<StackReclaimPass> {
|
|
public:
|
|
using StackReclaimBase<StackReclaimPass>::StackReclaimBase;
|
|
|
|
void runOnOperation() override;
|
|
};
|
|
} // namespace
|
|
|
|
uint64_t getAllocaAddressSpace(Operation *op) {
|
|
mlir::ModuleOp module = mlir::dyn_cast_or_null<mlir::ModuleOp>(op);
|
|
if (!module)
|
|
module = op->getParentOfType<mlir::ModuleOp>();
|
|
|
|
if (mlir::Attribute addrSpace =
|
|
mlir::DataLayout(module).getAllocaMemorySpace())
|
|
return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt();
|
|
return 0;
|
|
}
|
|
|
|
void StackReclaimPass::runOnOperation() {
|
|
auto *op = getOperation();
|
|
auto *context = &getContext();
|
|
mlir::OpBuilder builder(context);
|
|
mlir::Type voidPtr =
|
|
mlir::LLVM::LLVMPointerType::get(context, getAllocaAddressSpace(op));
|
|
|
|
op->walk([&](fir::DoLoopOp loopOp) {
|
|
mlir::Location loc = loopOp.getLoc();
|
|
|
|
if (!loopOp.getRegion().getOps<fir::AllocaOp>().empty()) {
|
|
builder.setInsertionPointToStart(&loopOp.getRegion().front());
|
|
auto stackSaveOp = builder.create<LLVM::StackSaveOp>(loc, voidPtr);
|
|
|
|
auto *terminator = loopOp.getRegion().back().getTerminator();
|
|
builder.setInsertionPoint(terminator);
|
|
builder.create<LLVM::StackRestoreOp>(loc, stackSaveOp);
|
|
}
|
|
});
|
|
}
|