forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial changes picked over from llvm#109833
- Loading branch information
Showing
9 changed files
with
180 additions
and
42 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
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
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
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