-
Notifications
You must be signed in to change notification settings - Fork 12.3k
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
[LLVM][IR] Add native vector support to ConstantInt & ConstantFP. #74502
Merged
paulwalker-arm
merged 3 commits into
llvm:main
from
paulwalker-arm:constant-vector-splats
Feb 22, 2024
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
1a494ec
[LLVM][IR] Add native vector support to ConstantInt & ConstantFP.
paulwalker-arm 27b6ede
Make ElementCount get interfaces private. Reduce repeated calls to ge…
paulwalker-arm ef185d1
Canonicalise splat like ConstantVectors to splat(value). Also adds ex…
paulwalker-arm 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 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 |
---|---|---|
|
@@ -35,6 +35,20 @@ | |
using namespace llvm; | ||
using namespace PatternMatch; | ||
|
||
// As set of temporary options to help migrate how splats are represented. | ||
static cl::opt<bool> UseConstantIntForFixedLengthSplat( | ||
"use-constant-int-for-fixed-length-splat", cl::init(false), cl::Hidden, | ||
cl::desc("Use ConstantInt's native fixed-length vector splat support.")); | ||
static cl::opt<bool> UseConstantFPForFixedLengthSplat( | ||
"use-constant-fp-for-fixed-length-splat", cl::init(false), cl::Hidden, | ||
cl::desc("Use ConstantFP's native fixed-length vector splat support.")); | ||
static cl::opt<bool> UseConstantIntForScalableSplat( | ||
"use-constant-int-for-scalable-splat", cl::init(false), cl::Hidden, | ||
cl::desc("Use ConstantInt's native scalable vector splat support.")); | ||
static cl::opt<bool> UseConstantFPForScalableSplat( | ||
"use-constant-fp-for-scalable-splat", cl::init(false), cl::Hidden, | ||
cl::desc("Use ConstantFP's native scalable vector splat support.")); | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Constant Class | ||
//===----------------------------------------------------------------------===// | ||
|
@@ -825,9 +839,11 @@ bool Constant::isManifestConstant() const { | |
// ConstantInt | ||
//===----------------------------------------------------------------------===// | ||
|
||
ConstantInt::ConstantInt(IntegerType *Ty, const APInt &V) | ||
ConstantInt::ConstantInt(Type *Ty, const APInt &V) | ||
: ConstantData(Ty, ConstantIntVal), Val(V) { | ||
assert(V.getBitWidth() == Ty->getBitWidth() && "Invalid constant for type"); | ||
assert(V.getBitWidth() == | ||
cast<IntegerType>(Ty->getScalarType())->getBitWidth() && | ||
"Invalid constant for type"); | ||
} | ||
|
||
ConstantInt *ConstantInt::getTrue(LLVMContext &Context) { | ||
|
@@ -885,6 +901,26 @@ ConstantInt *ConstantInt::get(LLVMContext &Context, const APInt &V) { | |
return Slot.get(); | ||
} | ||
|
||
// Get a ConstantInt vector with each lane set to the same APInt. | ||
ConstantInt *ConstantInt::get(LLVMContext &Context, ElementCount EC, | ||
const APInt &V) { | ||
nikic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Get an existing value or the insertion position. | ||
std::unique_ptr<ConstantInt> &Slot = | ||
Context.pImpl->IntSplatConstants[std::make_pair(EC, V)]; | ||
if (!Slot) { | ||
IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); | ||
VectorType *VTy = VectorType::get(ITy, EC); | ||
Slot.reset(new ConstantInt(VTy, V)); | ||
} | ||
|
||
#ifndef NDEBUG | ||
IntegerType *ITy = IntegerType::get(Context, V.getBitWidth()); | ||
VectorType *VTy = VectorType::get(ITy, EC); | ||
assert(Slot->getType() == VTy); | ||
#endif | ||
return Slot.get(); | ||
} | ||
|
||
Constant *ConstantInt::get(Type *Ty, uint64_t V, bool isSigned) { | ||
Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned); | ||
|
||
|
@@ -1024,6 +1060,26 @@ ConstantFP* ConstantFP::get(LLVMContext &Context, const APFloat& V) { | |
return Slot.get(); | ||
} | ||
|
||
// Get a ConstantFP vector with each lane set to the same APFloat. | ||
ConstantFP *ConstantFP::get(LLVMContext &Context, ElementCount EC, | ||
const APFloat &V) { | ||
// Get an existing value or the insertion position. | ||
std::unique_ptr<ConstantFP> &Slot = | ||
Context.pImpl->FPSplatConstants[std::make_pair(EC, V)]; | ||
if (!Slot) { | ||
Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics()); | ||
VectorType *VTy = VectorType::get(EltTy, EC); | ||
Slot.reset(new ConstantFP(VTy, V)); | ||
} | ||
|
||
#ifndef NDEBUG | ||
Type *EltTy = Type::getFloatingPointTy(Context, V.getSemantics()); | ||
VectorType *VTy = VectorType::get(EltTy, EC); | ||
assert(Slot->getType() == VTy); | ||
#endif | ||
return Slot.get(); | ||
} | ||
|
||
Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) { | ||
const fltSemantics &Semantics = Ty->getScalarType()->getFltSemantics(); | ||
Constant *C = get(Ty->getContext(), APFloat::getInf(Semantics, Negative)); | ||
|
@@ -1036,7 +1092,7 @@ Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) { | |
|
||
ConstantFP::ConstantFP(Type *Ty, const APFloat &V) | ||
: ConstantData(Ty, ConstantFPVal), Val(V) { | ||
assert(&V.getSemantics() == &Ty->getFltSemantics() && | ||
assert(&V.getSemantics() == &Ty->getScalarType()->getFltSemantics() && | ||
"FP type Mismatch"); | ||
} | ||
|
||
|
@@ -1384,6 +1440,16 @@ Constant *ConstantVector::getImpl(ArrayRef<Constant*> V) { | |
|
||
Constant *ConstantVector::getSplat(ElementCount EC, Constant *V) { | ||
if (!EC.isScalable()) { | ||
// Maintain special handling of zero. | ||
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. Just wondering whether this is something you want to keep long term or just initially? 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. I'm happy to take offers but see it as temporary. |
||
if (!V->isNullValue()) { | ||
if (UseConstantIntForFixedLengthSplat && isa<ConstantInt>(V)) | ||
return ConstantInt::get(V->getContext(), EC, | ||
cast<ConstantInt>(V)->getValue()); | ||
if (UseConstantFPForFixedLengthSplat && isa<ConstantFP>(V)) | ||
return ConstantFP::get(V->getContext(), EC, | ||
cast<ConstantFP>(V)->getValue()); | ||
} | ||
|
||
// If this splat is compatible with ConstantDataVector, use it instead of | ||
// ConstantVector. | ||
if ((isa<ConstantFP>(V) || isa<ConstantInt>(V)) && | ||
|
@@ -1394,6 +1460,16 @@ Constant *ConstantVector::getSplat(ElementCount EC, Constant *V) { | |
return get(Elts); | ||
} | ||
|
||
// Maintain special handling of zero. | ||
if (!V->isNullValue()) { | ||
if (UseConstantIntForScalableSplat && isa<ConstantInt>(V)) | ||
return ConstantInt::get(V->getContext(), EC, | ||
cast<ConstantInt>(V)->getValue()); | ||
if (UseConstantFPForScalableSplat && isa<ConstantFP>(V)) | ||
return ConstantFP::get(V->getContext(), EC, | ||
cast<ConstantFP>(V)->getValue()); | ||
} | ||
|
||
Type *VTy = VectorType::get(V->getType(), EC); | ||
|
||
if (V->isNullValue()) | ||
|
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,61 @@ | ||
; RUN: llvm-as -use-constant-int-for-fixed-length-splat \ | ||
; RUN: -use-constant-fp-for-fixed-length-splat \ | ||
; RUN: -use-constant-int-for-scalable-splat \ | ||
; RUN: -use-constant-fp-for-scalable-splat \ | ||
; RUN: < %s | llvm-dis -use-constant-int-for-fixed-length-splat \ | ||
; RUN: -use-constant-fp-for-fixed-length-splat \ | ||
; RUN: -use-constant-int-for-scalable-splat \ | ||
; RUN: -use-constant-fp-for-scalable-splat \ | ||
; RUN: | FileCheck %s | ||
|
||
; CHECK: @constant.splat.i1 = constant <1 x i1> splat (i1 true) | ||
@constant.splat.i1 = constant <1 x i1> splat (i1 true) | ||
|
||
; CHECK: @constant.splat.i32 = constant <5 x i32> splat (i32 7) | ||
@constant.splat.i32 = constant <5 x i32> splat (i32 7) | ||
|
||
; CHECK: @constant.splat.i128 = constant <7 x i128> splat (i128 85070591730234615870450834276742070272) | ||
@constant.splat.i128 = constant <7 x i128> splat (i128 85070591730234615870450834276742070272) | ||
|
||
; CHECK: @constant.splat.f16 = constant <2 x half> splat (half 0xHBC00) | ||
@constant.splat.f16 = constant <2 x half> splat (half 0xHBC00) | ||
|
||
; CHECK: @constant.splat.f32 = constant <4 x float> splat (float -2.000000e+00) | ||
@constant.splat.f32 = constant <4 x float> splat (float -2.000000e+00) | ||
|
||
; CHECK: @constant.splat.f64 = constant <6 x double> splat (double -3.000000e+00) | ||
@constant.splat.f64 = constant <6 x double> splat (double -3.000000e+00) | ||
|
||
; CHECK: @constant.splat.128 = constant <8 x fp128> splat (fp128 0xL00000000000000018000000000000000) | ||
@constant.splat.128 = constant <8 x fp128> splat (fp128 0xL00000000000000018000000000000000) | ||
|
||
; CHECK: @constant.splat.bf16 = constant <1 x bfloat> splat (bfloat 0xRC0A0) | ||
@constant.splat.bf16 = constant <1 x bfloat> splat (bfloat 0xRC0A0) | ||
|
||
; CHECK: @constant.splat.x86_fp80 = constant <3 x x86_fp80> splat (x86_fp80 0xK4000C8F5C28F5C28F800) | ||
@constant.splat.x86_fp80 = constant <3 x x86_fp80> splat (x86_fp80 0xK4000C8F5C28F5C28F800) | ||
|
||
; CHECK: @constant.splat.ppc_fp128 = constant <7 x ppc_fp128> splat (ppc_fp128 0xM80000000000000000000000000000000) | ||
@constant.splat.ppc_fp128 = constant <7 x ppc_fp128> splat (ppc_fp128 0xM80000000000000000000000000000000) | ||
|
||
define void @add_fixed_lenth_vector_splat_i32(<4 x i32> %a) { | ||
; CHECK: %add = add <4 x i32> %a, splat (i32 137) | ||
%add = add <4 x i32> %a, splat (i32 137) | ||
ret void | ||
} | ||
|
||
define <4 x i32> @ret_fixed_lenth_vector_splat_i32() { | ||
; CHECK: ret <4 x i32> splat (i32 56) | ||
ret <4 x i32> splat (i32 56) | ||
} | ||
|
||
define void @add_fixed_lenth_vector_splat_double(<vscale x 2 x double> %a) { | ||
; CHECK: %add = fadd <vscale x 2 x double> %a, splat (double 5.700000e+00) | ||
%add = fadd <vscale x 2 x double> %a, splat (double 5.700000e+00) | ||
ret void | ||
} | ||
|
||
define <vscale x 4 x i32> @ret_scalable_vector_splat_i32() { | ||
; CHECK: ret <vscale x 4 x i32> splat (i32 78) | ||
ret <vscale x 4 x i32> splat (i32 78) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paulwalker-arm @nikic Do you think it makes sense to make these available in a header file? I tried to set them directly from within a custom tool, but failed to get access to these. For us, setting them via. the command line is impractical.
If so, I can provide a PR that moves their declaration to a header file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say tentatively "no". We only really export options if we need to read them from multiple files. If we wanted to make these settable via API, I think the way we'd typically do that is by moving them into LLVMContext and adding setters.
FWIW, I don't think these options are mature enough yet for practical usage.
Can you provide more details on this? The usual way to set the options is via cl::ParseCommandLineOptions, which doesn't mean that they actually have to come from the command line. It's pretty typical to just hardcode some options in the invocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fast response.
I attempted to directly access the underlying
Option
structures, but that did not succeed.I was not aware that one can executed this function twice to pass custom options to it, thanks for the hint. This should work, once some other, only tangentially related, issues have been addressed.