-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[MLIR][GPU-LLVM] Convert gpu.func
to llvm.func
#101664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
victor-eds
merged 10 commits into
llvm:main
from
victor-eds:gpu-func-llvm-spv-conversion
Aug 9, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
08332a6
[MLIR][GPU-LLVM] Convert `gpu.func` to `llvm.func`
victor-eds 098af95
Apply suggestions and implement `llvm.mlir.workgroup_attrib_size`
victor-eds 36d5bf0
Use tuple to encode workgroup attribution in LLVM
victor-eds ed7b600
Add doc
victor-eds 81bf21c
Use `discardableAttrs`
victor-eds 361c336
`attrib->attribution`
victor-eds bf25aec
Change doc
victor-eds 3981cc8
Address comments
victor-eds af5955a
Improve doc
victor-eds 512724f
Format tests
victor-eds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
mlir/include/mlir/Conversion/SPIRVCommon/AttrToLLVMConverter.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===- AttrToLLVMConverter.h - SPIR-V attributes conversion to LLVM - C++ -===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
#ifndef MLIR_CONVERSION_SPIRVCOMMON_ATTRTOLLVMCONVERTER_H_ | ||
#define MLIR_CONVERSION_SPIRVCOMMON_ATTRTOLLVMCONVERTER_H_ | ||
|
||
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h" | ||
|
||
namespace mlir { | ||
unsigned storageClassToAddressSpace(spirv::ClientAPI clientAPI, | ||
spirv::StorageClass storageClass); | ||
} // namespace mlir | ||
|
||
#endif // MLIR_CONVERSION_SPIRVCOMMON_ATTRTOLLVMCONVERTER_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,29 +25,80 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, | |
Location loc = gpuFuncOp.getLoc(); | ||
|
||
SmallVector<LLVM::GlobalOp, 3> workgroupBuffers; | ||
workgroupBuffers.reserve(gpuFuncOp.getNumWorkgroupAttributions()); | ||
for (const auto [idx, attribution] : | ||
llvm::enumerate(gpuFuncOp.getWorkgroupAttributions())) { | ||
auto type = dyn_cast<MemRefType>(attribution.getType()); | ||
assert(type && type.hasStaticShape() && "unexpected type in attribution"); | ||
|
||
uint64_t numElements = type.getNumElements(); | ||
|
||
auto elementType = | ||
cast<Type>(typeConverter->convertType(type.getElementType())); | ||
auto arrayType = LLVM::LLVMArrayType::get(elementType, numElements); | ||
std::string name = | ||
std::string(llvm::formatv("__wg_{0}_{1}", gpuFuncOp.getName(), idx)); | ||
uint64_t alignment = 0; | ||
if (auto alignAttr = | ||
dyn_cast_or_null<IntegerAttr>(gpuFuncOp.getWorkgroupAttributionAttr( | ||
idx, LLVM::LLVMDialect::getAlignAttrName()))) | ||
alignment = alignAttr.getInt(); | ||
auto globalOp = rewriter.create<LLVM::GlobalOp>( | ||
gpuFuncOp.getLoc(), arrayType, /*isConstant=*/false, | ||
LLVM::Linkage::Internal, name, /*value=*/Attribute(), alignment, | ||
workgroupAddrSpace); | ||
workgroupBuffers.push_back(globalOp); | ||
if (encodeWorkgroupAttributionsAsArguments) { | ||
victor-eds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Append an `llvm.ptr` argument to the function signature to encode | ||
// workgroup attributions. | ||
|
||
ArrayRef<BlockArgument> workgroupAttributions = | ||
gpuFuncOp.getWorkgroupAttributions(); | ||
size_t numAttributions = workgroupAttributions.size(); | ||
|
||
// Insert all arguments at the end. | ||
unsigned index = gpuFuncOp.getNumArguments(); | ||
SmallVector<unsigned> argIndices(numAttributions, index); | ||
|
||
// New arguments will simply be `llvm.ptr` with the correct address space | ||
Type workgroupPtrType = | ||
rewriter.getType<LLVM::LLVMPointerType>(workgroupAddrSpace); | ||
SmallVector<Type> argTypes(numAttributions, workgroupPtrType); | ||
|
||
// Attributes: noalias, llvm.mlir.workgroup_attribution(<size>, <type>) | ||
std::array attrs{ | ||
rewriter.getNamedAttr(LLVM::LLVMDialect::getNoAliasAttrName(), | ||
rewriter.getUnitAttr()), | ||
rewriter.getNamedAttr( | ||
getDialect().getWorkgroupAttributionAttrHelper().getName(), | ||
rewriter.getUnitAttr()), | ||
}; | ||
SmallVector<DictionaryAttr> argAttrs; | ||
for (BlockArgument attribution : workgroupAttributions) { | ||
auto attributionType = cast<MemRefType>(attribution.getType()); | ||
IntegerAttr numElements = | ||
rewriter.getI64IntegerAttr(attributionType.getNumElements()); | ||
Type llvmElementType = | ||
getTypeConverter()->convertType(attributionType.getElementType()); | ||
if (!llvmElementType) | ||
return failure(); | ||
TypeAttr type = TypeAttr::get(llvmElementType); | ||
attrs.back().setValue( | ||
rewriter.getAttr<LLVM::WorkgroupAttributionAttr>(numElements, type)); | ||
argAttrs.push_back(rewriter.getDictionaryAttr(attrs)); | ||
} | ||
|
||
// Location match function location | ||
SmallVector<Location> argLocs(numAttributions, gpuFuncOp.getLoc()); | ||
|
||
// Perform signature modification | ||
rewriter.modifyOpInPlace( | ||
gpuFuncOp, [gpuFuncOp, &argIndices, &argTypes, &argAttrs, &argLocs]() { | ||
static_cast<FunctionOpInterface>(gpuFuncOp).insertArguments( | ||
argIndices, argTypes, argAttrs, argLocs); | ||
}); | ||
} else { | ||
workgroupBuffers.reserve(gpuFuncOp.getNumWorkgroupAttributions()); | ||
for (auto [idx, attribution] : | ||
llvm::enumerate(gpuFuncOp.getWorkgroupAttributions())) { | ||
auto type = dyn_cast<MemRefType>(attribution.getType()); | ||
assert(type && type.hasStaticShape() && "unexpected type in attribution"); | ||
|
||
uint64_t numElements = type.getNumElements(); | ||
|
||
auto elementType = | ||
cast<Type>(typeConverter->convertType(type.getElementType())); | ||
auto arrayType = LLVM::LLVMArrayType::get(elementType, numElements); | ||
std::string name = | ||
std::string(llvm::formatv("__wg_{0}_{1}", gpuFuncOp.getName(), idx)); | ||
uint64_t alignment = 0; | ||
if (auto alignAttr = dyn_cast_or_null<IntegerAttr>( | ||
gpuFuncOp.getWorkgroupAttributionAttr( | ||
idx, LLVM::LLVMDialect::getAlignAttrName()))) | ||
alignment = alignAttr.getInt(); | ||
auto globalOp = rewriter.create<LLVM::GlobalOp>( | ||
gpuFuncOp.getLoc(), arrayType, /*isConstant=*/false, | ||
LLVM::Linkage::Internal, name, /*value=*/Attribute(), alignment, | ||
workgroupAddrSpace); | ||
workgroupBuffers.push_back(globalOp); | ||
} | ||
} | ||
|
||
// Remap proper input types. | ||
|
@@ -101,16 +152,19 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, | |
// attribute. The former is necessary for further translation while the | ||
// latter is expected by gpu.launch_func. | ||
if (gpuFuncOp.isKernel()) { | ||
attributes.emplace_back(kernelAttributeName, rewriter.getUnitAttr()); | ||
if (kernelAttributeName) | ||
attributes.emplace_back(kernelAttributeName, rewriter.getUnitAttr()); | ||
// Set the dialect-specific block size attribute if there is one. | ||
if (kernelBlockSizeAttributeName.has_value() && knownBlockSize) { | ||
attributes.emplace_back(kernelBlockSizeAttributeName.value(), | ||
knownBlockSize); | ||
if (kernelBlockSizeAttributeName && knownBlockSize) { | ||
attributes.emplace_back(kernelBlockSizeAttributeName, knownBlockSize); | ||
} | ||
} | ||
LLVM::CConv callingConvention = gpuFuncOp.isKernel() | ||
? kernelCallingConvention | ||
: nonKernelCallingConvention; | ||
auto llvmFuncOp = rewriter.create<LLVM::LLVMFuncOp>( | ||
gpuFuncOp.getLoc(), gpuFuncOp.getName(), funcType, | ||
LLVM::Linkage::External, /*dsoLocal=*/false, /*cconv=*/LLVM::CConv::C, | ||
LLVM::Linkage::External, /*dsoLocal=*/false, callingConvention, | ||
/*comdat=*/nullptr, attributes); | ||
|
||
{ | ||
|
@@ -125,24 +179,51 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, | |
rewriter.setInsertionPointToStart(&gpuFuncOp.front()); | ||
unsigned numProperArguments = gpuFuncOp.getNumArguments(); | ||
|
||
for (const auto [idx, global] : llvm::enumerate(workgroupBuffers)) { | ||
auto ptrType = LLVM::LLVMPointerType::get(rewriter.getContext(), | ||
global.getAddrSpace()); | ||
Value address = rewriter.create<LLVM::AddressOfOp>( | ||
loc, ptrType, global.getSymNameAttr()); | ||
Value memory = | ||
rewriter.create<LLVM::GEPOp>(loc, ptrType, global.getType(), address, | ||
ArrayRef<LLVM::GEPArg>{0, 0}); | ||
|
||
// Build a memref descriptor pointing to the buffer to plug with the | ||
// existing memref infrastructure. This may use more registers than | ||
// otherwise necessary given that memref sizes are fixed, but we can try | ||
// and canonicalize that away later. | ||
Value attribution = gpuFuncOp.getWorkgroupAttributions()[idx]; | ||
auto type = cast<MemRefType>(attribution.getType()); | ||
auto descr = MemRefDescriptor::fromStaticShape( | ||
rewriter, loc, *getTypeConverter(), type, memory); | ||
signatureConversion.remapInput(numProperArguments + idx, descr); | ||
if (encodeWorkgroupAttributionsAsArguments) { | ||
victor-eds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Build a MemRefDescriptor with each of the arguments added above. | ||
|
||
unsigned numAttributions = gpuFuncOp.getNumWorkgroupAttributions(); | ||
assert(numProperArguments >= numAttributions && | ||
"Expecting attributions to be encoded as arguments already"); | ||
|
||
// Arguments encoding workgroup attributions will be in positions | ||
// [numProperArguments, numProperArguments+numAttributions) | ||
ArrayRef<BlockArgument> attributionArguments = | ||
gpuFuncOp.getArguments().slice(numProperArguments - numAttributions, | ||
numAttributions); | ||
for (auto [idx, vals] : llvm::enumerate(llvm::zip_equal( | ||
gpuFuncOp.getWorkgroupAttributions(), attributionArguments))) { | ||
auto [attribution, arg] = vals; | ||
auto type = cast<MemRefType>(attribution.getType()); | ||
|
||
// Arguments are of llvm.ptr type and attributions are of memref type: | ||
// we need to wrap them in memref descriptors. | ||
Value descr = MemRefDescriptor::fromStaticShape( | ||
rewriter, loc, *getTypeConverter(), type, arg); | ||
|
||
// And remap the arguments | ||
signatureConversion.remapInput(numProperArguments + idx, descr); | ||
} | ||
} else { | ||
for (const auto [idx, global] : llvm::enumerate(workgroupBuffers)) { | ||
victor-eds marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto ptrType = LLVM::LLVMPointerType::get(rewriter.getContext(), | ||
global.getAddrSpace()); | ||
Value address = rewriter.create<LLVM::AddressOfOp>( | ||
loc, ptrType, global.getSymNameAttr()); | ||
Value memory = | ||
rewriter.create<LLVM::GEPOp>(loc, ptrType, global.getType(), | ||
address, ArrayRef<LLVM::GEPArg>{0, 0}); | ||
|
||
// Build a memref descriptor pointing to the buffer to plug with the | ||
// existing memref infrastructure. This may use more registers than | ||
// otherwise necessary given that memref sizes are fixed, but we can try | ||
// and canonicalize that away later. | ||
Value attribution = gpuFuncOp.getWorkgroupAttributions()[idx]; | ||
auto type = cast<MemRefType>(attribution.getType()); | ||
auto descr = MemRefDescriptor::fromStaticShape( | ||
rewriter, loc, *getTypeConverter(), type, memory); | ||
signatureConversion.remapInput(numProperArguments + idx, descr); | ||
} | ||
Comment on lines
+208
to
+226
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Original code |
||
} | ||
|
||
// Rewrite private memory attributions to alloca'ed buffers. | ||
|
@@ -239,6 +320,8 @@ GPUFuncOpLowering::matchAndRewrite(gpu::GPUFuncOp gpuFuncOp, OpAdaptor adaptor, | |
copyPointerAttribute(LLVM::LLVMDialect::getDereferenceableAttrName()); | ||
copyPointerAttribute( | ||
LLVM::LLVMDialect::getDereferenceableOrNullAttrName()); | ||
copyPointerAttribute( | ||
LLVM::LLVMDialect::WorkgroupAttributionAttrHelper::getNameStr()); | ||
} | ||
} | ||
rewriter.eraseOp(gpuFuncOp); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.