Files
TheRock/compiler/amd-llvm/mlir/lib/Dialect/OpenMP/Transforms/StackToShared.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

126 lines
5.0 KiB
C++

//===- StackToShared.cpp -------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file implements transforms to swap stack allocations on the target
// device with device shared memory where applicable.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/OpenMP/Transforms/Passes.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/Dialect/OpenMP/Utils/Utils.h"
#include "mlir/Pass/Pass.h"
#include "llvm/ADT/STLExtras.h"
namespace mlir {
namespace omp {
#define GEN_PASS_DEF_STACKTOSHAREDPASS
#include "mlir/Dialect/OpenMP/Transforms/Passes.h.inc"
} // namespace omp
} // namespace mlir
using namespace mlir;
/// Tell whether to replace an operation representing a stack allocation with a
/// device shared memory allocation/deallocation pair based on the location of
/// the allocation and its uses.
static bool shouldReplaceAllocaWithDeviceSharedMem(Operation &op) {
return omp::opInSharedDeviceContext(op) &&
llvm::any_of(op.getResults(), [&](Value result) {
return omp::allocaUsesRequireSharedMem(result);
});
}
/// Based on the location of the definition of the given value representing the
/// result of a device shared memory allocation, find the corresponding points
/// where its deallocation should be placed and introduce `omp.free_shared_mem`
/// ops at those points.
static void insertDeviceSharedMemDeallocation(OpBuilder &builder,
TypeAttr elemType,
Value arraySize,
IntegerAttr alignment,
Value allocVal) {
Block *allocaBlock = allocVal.getParentBlock();
DominanceInfo domInfo;
for (Block &block : allocVal.getParentRegion()->getBlocks()) {
Operation *terminator = block.getTerminator();
if (!terminator->hasSuccessors() &&
domInfo.dominates(allocaBlock, &block)) {
builder.setInsertionPoint(terminator);
omp::FreeSharedMemOp::create(builder, allocVal.getLoc(), elemType,
arraySize, alignment, allocVal);
}
}
}
namespace {
class StackToSharedPass
: public omp::impl::StackToSharedPassBase<StackToSharedPass> {
public:
StackToSharedPass() = default;
void runOnOperation() override {
MLIRContext *context = &getContext();
OpBuilder builder(context);
LLVM::LLVMFuncOp funcOp = getOperation();
auto offloadIface = funcOp->getParentOfType<omp::OffloadModuleInterface>();
if (!offloadIface || !offloadIface.getIsTargetDevice())
return;
llvm::SmallVector<Operation *> toBeDeleted;
funcOp->walk([&](LLVM::AllocaOp allocaOp) {
if (!shouldReplaceAllocaWithDeviceSharedMem(*allocaOp))
return;
// Replace llvm.alloca with omp.alloc_shared_mem.
Type resultType = allocaOp.getResult().getType();
// TODO: The handling of non-default address spaces might need to be
// improved. This currently only handles the case where an alloca to
// non-default address space is only used by a single addrspacecast to
// default address space.
bool nonDefaultAddrSpace = false;
if (auto llvmPtrType = dyn_cast<LLVM::LLVMPointerType>(resultType))
nonDefaultAddrSpace = llvmPtrType.getAddressSpace() != 0;
builder.setInsertionPoint(allocaOp);
auto sharedAllocOp = omp::AllocSharedMemOp::create(
builder, allocaOp->getLoc(), LLVM::LLVMPointerType::get(context),
allocaOp.getElemTypeAttr(), allocaOp.getArraySize(),
allocaOp.getAlignmentAttr());
if (nonDefaultAddrSpace) {
assert(allocaOp->hasOneUse() && " unsupported non-default address "
"space alloca with multiple uses");
auto asCastOp =
cast<LLVM::AddrSpaceCastOp>(*allocaOp->getUsers().begin());
asCastOp.replaceAllUsesWith(sharedAllocOp.getOperation());
// Delete later because we can't delete the cast op before the top-level
// iteration visits it. Also, the alloca can't be deleted before because
// it's used by it.
toBeDeleted.push_back(asCastOp);
toBeDeleted.push_back(allocaOp);
} else {
allocaOp.replaceAllUsesWith(sharedAllocOp.getOperation());
allocaOp.erase();
}
// Create a new omp.free_shared_mem for the allocated buffer prior to
// exiting the region.
insertDeviceSharedMemDeallocation(
builder, sharedAllocOp.getMemElemTypeAttr(),
sharedAllocOp.getMemArraySize(), sharedAllocOp.getMemAlignmentAttr(),
sharedAllocOp.getResult());
});
for (Operation *op : toBeDeleted)
op->erase();
}
};
} // namespace