-
Notifications
You must be signed in to change notification settings - Fork 14.2k
[VFABI] Add support for vector functions that return struct types #119000
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16afa09
[VFABI] Add support for vector functions that return struct types
MacDue 349b678
Fixups
MacDue 8199c23
Rm include
MacDue 7b23397
Update comment
MacDue 0bb3c80
Fixups
MacDue 54c6244
Fix typo
MacDue 91085c6
Fixups
MacDue 899f19a
Move helpers
MacDue ee19f4e
Fixups
MacDue b0ff8df
RM
MacDue 5c155ce
RM
MacDue dd90ff4
RM
MacDue cbf5ff9
fix typo
MacDue 39c5bc4
widened type -> vectorized type
MacDue 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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//===------- VectorTypeUtils.h - Vector type utility functions -*- 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 LLVM_IR_VECTORTYPEUTILS_H | ||
#define LLVM_IR_VECTORTYPEUTILS_H | ||
|
||
#include "llvm/IR/DerivedTypes.h" | ||
|
||
namespace llvm { | ||
|
||
/// A helper function for converting Scalar types to vector types. If | ||
/// the incoming type is void, we return void. If the EC represents a | ||
/// scalar, we return the scalar type. | ||
inline Type *ToVectorTy(Type *Scalar, ElementCount EC) { | ||
if (Scalar->isVoidTy() || Scalar->isMetadataTy() || EC.isScalar()) | ||
return Scalar; | ||
return VectorType::get(Scalar, EC); | ||
} | ||
|
||
inline Type *ToVectorTy(Type *Scalar, unsigned VF) { | ||
return ToVectorTy(Scalar, ElementCount::getFixed(VF)); | ||
} | ||
|
||
/// A helper for converting structs of scalar types to structs of vector types. | ||
/// Note: | ||
/// - If \p EC is scalar, \p StructTy is returned unchanged | ||
/// - Only unpacked literal struct types are supported | ||
Type *toVectorizedStructTy(StructType *StructTy, ElementCount EC); | ||
|
||
/// A helper for converting structs of vector types to structs of scalar types. | ||
/// Note: Only unpacked literal struct types are supported. | ||
Type *toScalarizedStructTy(StructType *StructTy); | ||
|
||
/// Returns true if `StructTy` is an unpacked literal struct where all elements | ||
/// are vectors of matching element count. This does not include empty structs. | ||
bool isVectorizedStructTy(StructType *StructTy); | ||
|
||
/// A helper for converting to vectorized types. For scalar types, this is | ||
/// equivalent to calling `ToVectorTy`. For struct types, this returns a new | ||
/// struct where each element type has been widened to a vector type. | ||
/// Note: | ||
/// - If the incoming type is void, we return void | ||
/// - If \p EC is scalar, \p Ty is returned unchanged | ||
/// - Only unpacked literal struct types are supported | ||
inline Type *toVectorizedTy(Type *Ty, ElementCount EC) { | ||
if (StructType *StructTy = dyn_cast<StructType>(Ty)) | ||
return toVectorizedStructTy(StructTy, EC); | ||
return ToVectorTy(Ty, EC); | ||
} | ||
|
||
/// A helper for converting vectorized types to scalarized (non-vector) types. | ||
/// For vector types, this is equivalent to calling .getScalarType(). For struct | ||
/// types, this returns a new struct where each element type has been converted | ||
/// to a scalar type. Note: Only unpacked literal struct types are supported. | ||
inline Type *toScalarizedTy(Type *Ty) { | ||
if (StructType *StructTy = dyn_cast<StructType>(Ty)) | ||
return toScalarizedStructTy(StructTy); | ||
return Ty->getScalarType(); | ||
} | ||
|
||
/// Returns true if `Ty` is a vector type or a struct of vector types where all | ||
/// vector types share the same VF. | ||
inline bool isVectorizedTy(Type *Ty) { | ||
if (StructType *StructTy = dyn_cast<StructType>(Ty)) | ||
return isVectorizedStructTy(StructTy); | ||
return Ty->isVectorTy(); | ||
} | ||
|
||
/// Returns the types contained in `Ty`. For struct types, it returns the | ||
/// elements, all other types are returned directly. | ||
inline ArrayRef<Type *> getContainedTypes(Type *const &Ty) { | ||
if (auto *StructTy = dyn_cast<StructType>(Ty)) | ||
return StructTy->elements(); | ||
return ArrayRef<Type *>(&Ty, 1); | ||
} | ||
|
||
/// Returns the number of vector elements for a vectorized type. | ||
inline ElementCount getVectorizedTypeVF(Type *Ty) { | ||
assert(isVectorizedTy(Ty) && "expected vectorized type"); | ||
return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount(); | ||
} | ||
|
||
inline bool isUnpackedStructLiteral(StructType *StructTy) { | ||
return StructTy->isLiteral() && !StructTy->isPacked(); | ||
} | ||
|
||
} // namespace llvm | ||
|
||
#endif |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===------- VectorTypeUtils.cpp - Vector type utility functions ----------===// | ||
// | ||
// 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 "llvm/IR/VectorTypeUtils.h" | ||
#include "llvm/ADT/SmallVectorExtras.h" | ||
|
||
using namespace llvm; | ||
|
||
/// A helper for converting structs of scalar types to structs of vector types. | ||
/// Note: Only unpacked literal struct types are supported. | ||
Type *llvm::toVectorizedStructTy(StructType *StructTy, ElementCount EC) { | ||
if (EC.isScalar()) | ||
return StructTy; | ||
assert(isUnpackedStructLiteral(StructTy) && | ||
"expected unpacked struct literal"); | ||
assert(all_of(StructTy->elements(), VectorType::isValidElementType) && | ||
"expected all element types to be valid vector element types"); | ||
return StructType::get( | ||
StructTy->getContext(), | ||
map_to_vector(StructTy->elements(), [&](Type *ElTy) -> Type * { | ||
return VectorType::get(ElTy, EC); | ||
})); | ||
} | ||
|
||
/// A helper for converting structs of vector types to structs of scalar types. | ||
/// Note: Only unpacked literal struct types are supported. | ||
Type *llvm::toScalarizedStructTy(StructType *StructTy) { | ||
assert(isUnpackedStructLiteral(StructTy) && | ||
"expected unpacked struct literal"); | ||
return StructType::get( | ||
StructTy->getContext(), | ||
map_to_vector(StructTy->elements(), [](Type *ElTy) -> Type * { | ||
return ElTy->getScalarType(); | ||
})); | ||
} | ||
|
||
/// Returns true if `StructTy` is an unpacked literal struct where all elements | ||
/// are vectors of matching element count. This does not include empty structs. | ||
bool llvm::isVectorizedStructTy(StructType *StructTy) { | ||
if (!isUnpackedStructLiteral(StructTy)) | ||
return false; | ||
auto ElemTys = StructTy->elements(); | ||
if (ElemTys.empty() || !ElemTys.front()->isVectorTy()) | ||
return false; | ||
ElementCount VF = cast<VectorType>(ElemTys.front())->getElementCount(); | ||
return all_of(ElemTys, [&](Type *Ty) { | ||
return Ty->isVectorTy() && cast<VectorType>(Ty)->getElementCount() == VF; | ||
}); | ||
} |
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.
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.