-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LV] Add initial support for vectorizing literal struct return values
This patch adds initial support for vectorizing literal struct return values. Currently, this is limited to the case where the struct is homogeneous (all elements have the same type) and not packed. The intended use case for this is vectorizing intrinsics such as: ``` declare { float, float } @llvm.sincos.f32(float %x) ``` Mapping them to structure-returning library calls such as: ``` declare { <4 x float>, <4 x i32> } @Sleef_sincosf4_u10advsimd(<4 x float>) ``` It could also be possible to vectorize the intrinsic (without a libcall) and then later lower the intrinsic to a library call. This may be desired if the only library calls available take output pointers rather than return multiple values. Implementing this required two main changes: 1. Supporting widening `extractvalue` 2. Adding support for "wide" types (in LV and parts of the cost model) The first change is relatively straightforward, the second is larger as it requires changing assumptions that types are always scalars or vectors. In this patch, a "wide" type is defined as a vector, or a struct literal where all elements are vectors (of the same element count). To help with the second change some helpers for wide types have been added (that work similarly to existing vector helpers). These have been used along the paths needed to support vectorizing calls, however, I expect there are many places that still only expect vector types.
- Loading branch information
Showing
16 changed files
with
506 additions
and
79 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,53 @@ | ||
//===----------- VectorUtils.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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/SmallVector.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 to wider (vector) 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: Only | ||
/// unpacked literal struct types are supported. | ||
Type *ToWideTy(Type *Ty, ElementCount EC); | ||
|
||
/// A helper for converting wide types to narrow (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. | ||
Type *ToNarrowTy(Type *Ty); | ||
|
||
/// Returns the types contained in `Ty`. For struct types, it returns the | ||
/// elements, all other types are returned directly. | ||
SmallVector<Type *, 2> getContainedTypes(Type *Ty); | ||
|
||
/// Returns true if `Ty` is a vector type or a struct of vector types where all | ||
/// vector types share the same VF. | ||
bool isWideTy(Type *Ty); | ||
|
||
/// Returns the vectorization factor for a widened type. | ||
inline ElementCount getWideTypeVF(Type *Ty) { | ||
assert(isWideTy(Ty) && "expected widened type!"); | ||
return cast<VectorType>(getContainedTypes(Ty).front())->getElementCount(); | ||
} | ||
|
||
} // namespace llvm |
This file contains 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 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 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 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 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,69 @@ | ||
//===----------- VectorUtils.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/VectorUtils.h" | ||
#include "llvm/ADT/SmallVectorExtras.h" | ||
|
||
using namespace llvm; | ||
|
||
/// A helper for converting to wider (vector) 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: Only | ||
/// unpacked literal struct types are supported. | ||
Type *llvm::ToWideTy(Type *Ty, ElementCount EC) { | ||
if (EC.isScalar()) | ||
return Ty; | ||
auto *StructTy = dyn_cast<StructType>(Ty); | ||
if (!StructTy) | ||
return ToVectorTy(Ty, EC); | ||
assert(StructTy->isLiteral() && !StructTy->isPacked() && | ||
"expected unpacked struct literal"); | ||
return StructType::get( | ||
Ty->getContext(), | ||
map_to_vector(StructTy->elements(), [&](Type *ElTy) -> Type * { | ||
return VectorType::get(ElTy, EC); | ||
})); | ||
} | ||
|
||
/// A helper for converting wide types to narrow (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. | ||
Type *llvm::ToNarrowTy(Type *Ty) { | ||
auto *StructTy = dyn_cast<StructType>(Ty); | ||
if (!StructTy) | ||
return Ty->getScalarType(); | ||
assert(StructTy->isLiteral() && !StructTy->isPacked() && | ||
"expected unpacked struct literal"); | ||
return StructType::get( | ||
Ty->getContext(), | ||
map_to_vector(StructTy->elements(), [](Type *ElTy) -> Type * { | ||
return ElTy->getScalarType(); | ||
})); | ||
} | ||
|
||
/// Returns the types contained in `Ty`. For struct types, it returns the | ||
/// elements, all other types are returned directly. | ||
SmallVector<Type *, 2> llvm::getContainedTypes(Type *Ty) { | ||
auto *StructTy = dyn_cast<StructType>(Ty); | ||
if (StructTy) | ||
return to_vector<2>(StructTy->elements()); | ||
return {Ty}; | ||
} | ||
|
||
/// Returns true if `Ty` is a vector type or a struct of vector types where all | ||
/// vector types share the same VF. | ||
bool llvm::isWideTy(Type *Ty) { | ||
auto ContainedTys = getContainedTypes(Ty); | ||
if (ContainedTys.empty() || !ContainedTys.front()->isVectorTy()) | ||
return false; | ||
ElementCount VF = cast<VectorType>(ContainedTys.front())->getElementCount(); | ||
return all_of(ContainedTys, [&](Type *Ty) { | ||
return Ty->isVectorTy() && cast<VectorType>(Ty)->getElementCount() == VF; | ||
}); | ||
} |
This file contains 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.