-
Notifications
You must be signed in to change notification settings - Fork 15.7k
[AArch64][Clang] Refactor code to emit SVE & SME builtins #70662
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
kmclaughlin-arm
merged 3 commits into
llvm:main
from
kmclaughlin-arm:sme2-refactor-builtin-exp
Nov 1, 2023
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9571,22 +9571,17 @@ Value *CodeGenFunction::EmitSVEStructStore(const SVETypeFlags &TypeFlags, | |
| Value *BasePtr = Ops[1]; | ||
|
|
||
| // Does the store have an offset? | ||
| if (Ops.size() > 3) | ||
| if (Ops.size() > (2 + N)) | ||
| BasePtr = Builder.CreateGEP(VTy, BasePtr, Ops[2]); | ||
|
|
||
| Value *Val = Ops.back(); | ||
|
|
||
| // The llvm.aarch64.sve.st2/3/4 intrinsics take legal part vectors, so we | ||
| // need to break up the tuple vector. | ||
| SmallVector<llvm::Value*, 5> Operands; | ||
| unsigned MinElts = VTy->getElementCount().getKnownMinValue(); | ||
| for (unsigned I = 0; I < N; ++I) { | ||
| Value *Idx = ConstantInt::get(CGM.Int64Ty, I * MinElts); | ||
| Operands.push_back(Builder.CreateExtractVector(VTy, Val, Idx)); | ||
| } | ||
| for (unsigned I = Ops.size() - N; I < Ops.size(); ++I) | ||
| Operands.push_back(Ops[I]); | ||
| Operands.append({Predicate, BasePtr}); | ||
|
|
||
| Function *F = CGM.getIntrinsic(IntID, { VTy }); | ||
|
|
||
| return Builder.CreateCall(F, Operands); | ||
| } | ||
|
|
||
|
|
@@ -9893,26 +9888,24 @@ Value *CodeGenFunction::FormSVEBuiltinResult(Value *Call) { | |
| return Call; | ||
| } | ||
|
|
||
| Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, | ||
| const CallExpr *E) { | ||
| void CodeGenFunction::GetAArch64SVEProcessedOperands( | ||
| unsigned BuiltinID, const CallExpr *E, SmallVectorImpl<Value *> &Ops, | ||
| SVETypeFlags TypeFlags) { | ||
| // Find out if any arguments are required to be integer constant expressions. | ||
| unsigned ICEArguments = 0; | ||
| ASTContext::GetBuiltinTypeError Error; | ||
| getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments); | ||
| assert(Error == ASTContext::GE_None && "Should not codegen an error"); | ||
|
|
||
| llvm::Type *Ty = ConvertType(E->getType()); | ||
| if (BuiltinID >= SVE::BI__builtin_sve_reinterpret_s8_s8 && | ||
| BuiltinID <= SVE::BI__builtin_sve_reinterpret_f64_f64) { | ||
| Value *Val = EmitScalarExpr(E->getArg(0)); | ||
| return EmitSVEReinterpret(Val, Ty); | ||
| } | ||
| // Tuple set/get only requires one insert/extract vector, which is | ||
| // created by EmitSVETupleSetOrGet. | ||
| bool IsTupleGetOrSet = TypeFlags.isTupleSet() || TypeFlags.isTupleGet(); | ||
|
|
||
| llvm::SmallVector<Value *, 4> Ops; | ||
| for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) { | ||
| if ((ICEArguments & (1 << i)) == 0) | ||
| Ops.push_back(EmitScalarExpr(E->getArg(i))); | ||
| else { | ||
| bool IsICE = ICEArguments & (1 << i); | ||
| Value *Arg = EmitScalarExpr(E->getArg(i)); | ||
|
|
||
| if (IsICE) { | ||
| // If this is required to be a constant, constant fold it so that we know | ||
| // that the generated intrinsic gets a ConstantInt. | ||
| std::optional<llvm::APSInt> Result = | ||
|
|
@@ -9924,12 +9917,56 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, | |
| // immediate requires more than a handful of bits. | ||
| *Result = Result->extOrTrunc(32); | ||
| Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), *Result)); | ||
| continue; | ||
| } | ||
|
|
||
| if (IsTupleGetOrSet) { | ||
| Ops.push_back(EmitScalarExpr(E->getArg(i))); | ||
| continue; | ||
| } | ||
|
|
||
| if (!isa<ScalableVectorType>(Arg->getType())) { | ||
| Ops.push_back(Arg); | ||
| continue; | ||
| } | ||
|
|
||
| auto *VTy = cast<ScalableVectorType>(Arg->getType()); | ||
| unsigned MinElts = VTy->getMinNumElements(); | ||
| bool IsPred = VTy->getElementType()->isIntegerTy(1); | ||
| unsigned N = (MinElts * VTy->getScalarSizeInBits()) / (IsPred ? 16 : 128); | ||
|
|
||
| if (N == 1) { | ||
| Ops.push_back(Arg); | ||
| continue; | ||
| } | ||
|
|
||
| for (unsigned I = 0; I < N; ++I) { | ||
| Value *Idx = ConstantInt::get(CGM.Int64Ty, (I * MinElts) / N); | ||
| auto *NewVTy = | ||
| ScalableVectorType::get(VTy->getElementType(), MinElts / N); | ||
| Ops.push_back(Builder.CreateExtractVector(NewVTy, Arg, Idx)); | ||
| } | ||
| } | ||
|
|
||
| return; | ||
|
||
| } | ||
|
|
||
| Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, | ||
| const CallExpr *E) { | ||
| llvm::Type *Ty = ConvertType(E->getType()); | ||
| if (BuiltinID >= SVE::BI__builtin_sve_reinterpret_s8_s8 && | ||
| BuiltinID <= SVE::BI__builtin_sve_reinterpret_f64_f64) { | ||
| Value *Val = EmitScalarExpr(E->getArg(0)); | ||
| return EmitSVEReinterpret(Val, Ty); | ||
| } | ||
|
|
||
| auto *Builtin = findARMVectorIntrinsicInMap(AArch64SVEIntrinsicMap, BuiltinID, | ||
| AArch64SVEIntrinsicsProvenSorted); | ||
|
|
||
| llvm::SmallVector<Value *, 4> Ops; | ||
| SVETypeFlags TypeFlags(Builtin->TypeModifier); | ||
| GetAArch64SVEProcessedOperands(BuiltinID, E, Ops, TypeFlags); | ||
|
|
||
| if (TypeFlags.isLoad()) | ||
| return EmitSVEMaskedLoad(E, Ty, Ops, Builtin->LLVMIntrinsic, | ||
| TypeFlags.isZExtReturn()); | ||
|
|
@@ -9943,14 +9980,14 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, | |
| return EmitSVEPrefetchLoad(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (TypeFlags.isGatherPrefetch()) | ||
| return EmitSVEGatherPrefetch(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (TypeFlags.isStructLoad()) | ||
| return EmitSVEStructLoad(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (TypeFlags.isStructStore()) | ||
| return EmitSVEStructStore(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (TypeFlags.isStructLoad()) | ||
| return EmitSVEStructLoad(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (TypeFlags.isStructStore()) | ||
| return EmitSVEStructStore(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (TypeFlags.isTupleSet() || TypeFlags.isTupleGet()) | ||
| return EmitSVETupleSetOrGet(TypeFlags, Ty, Ops); | ||
| return EmitSVETupleSetOrGet(TypeFlags, Ty, Ops); | ||
| else if (TypeFlags.isTupleCreate()) | ||
| return EmitSVETupleCreate(TypeFlags, Ty, Ops); | ||
| return EmitSVETupleCreate(TypeFlags, Ty, Ops); | ||
| else if (TypeFlags.isUndef()) | ||
| return UndefValue::get(Ty); | ||
| else if (Builtin->LLVMIntrinsic != 0) { | ||
|
|
@@ -10202,13 +10239,8 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, | |
| case SVE::BI__builtin_sve_svtbl2_f64: { | ||
| SVETypeFlags TF(Builtin->TypeModifier); | ||
| auto VTy = cast<llvm::ScalableVectorType>(getSVEType(TF)); | ||
| Value *V0 = Builder.CreateExtractVector(VTy, Ops[0], | ||
| ConstantInt::get(CGM.Int64Ty, 0)); | ||
| unsigned MinElts = VTy->getMinNumElements(); | ||
| Value *V1 = Builder.CreateExtractVector( | ||
| VTy, Ops[0], ConstantInt::get(CGM.Int64Ty, MinElts)); | ||
| Function *F = CGM.getIntrinsic(Intrinsic::aarch64_sve_tbl2, VTy); | ||
| return Builder.CreateCall(F, {V0, V1, Ops[1]}); | ||
| return Builder.CreateCall(F, Ops); | ||
| } | ||
|
|
||
| case SVE::BI__builtin_sve_svset_neonq_s8: | ||
|
|
@@ -10266,35 +10298,13 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, | |
|
|
||
| Value *CodeGenFunction::EmitAArch64SMEBuiltinExpr(unsigned BuiltinID, | ||
| const CallExpr *E) { | ||
| // Find out if any arguments are required to be integer constant expressions. | ||
| unsigned ICEArguments = 0; | ||
| ASTContext::GetBuiltinTypeError Error; | ||
| getContext().GetBuiltinType(BuiltinID, Error, &ICEArguments); | ||
| assert(Error == ASTContext::GE_None && "Should not codegen an error"); | ||
|
|
||
| llvm::Type *Ty = ConvertType(E->getType()); | ||
| llvm::SmallVector<Value *, 4> Ops; | ||
| for (unsigned i = 0, e = E->getNumArgs(); i != e; i++) { | ||
| if ((ICEArguments & (1 << i)) == 0) | ||
| Ops.push_back(EmitScalarExpr(E->getArg(i))); | ||
| else { | ||
| // If this is required to be a constant, constant fold it so that we know | ||
| // that the generated intrinsic gets a ConstantInt. | ||
| std::optional<llvm::APSInt> Result = | ||
| E->getArg(i)->getIntegerConstantExpr(getContext()); | ||
| assert(Result && "Expected argument to be a constant"); | ||
|
|
||
| // Immediates for SVE llvm intrinsics are always 32bit. We can safely | ||
| // truncate because the immediate has been range checked and no valid | ||
| // immediate requires more than a handful of bits. | ||
| *Result = Result->extOrTrunc(32); | ||
| Ops.push_back(llvm::ConstantInt::get(getLLVMContext(), *Result)); | ||
| } | ||
| } | ||
|
|
||
| auto *Builtin = findARMVectorIntrinsicInMap(AArch64SMEIntrinsicMap, BuiltinID, | ||
| AArch64SMEIntrinsicsProvenSorted); | ||
|
|
||
| llvm::SmallVector<Value *, 4> Ops; | ||
| SVETypeFlags TypeFlags(Builtin->TypeModifier); | ||
| GetAArch64SVEProcessedOperands(BuiltinID, E, Ops, TypeFlags); | ||
|
|
||
| if (TypeFlags.isLoad() || TypeFlags.isStore()) | ||
| return EmitSMELd1St1(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (TypeFlags.isReadZA() || TypeFlags.isWriteZA()) | ||
|
|
@@ -10307,21 +10317,24 @@ Value *CodeGenFunction::EmitAArch64SMEBuiltinExpr(unsigned BuiltinID, | |
| BuiltinID == SME::BI__builtin_sme_svldr_za || | ||
| BuiltinID == SME::BI__builtin_sme_svstr_za) | ||
| return EmitSMELdrStr(TypeFlags, Ops, Builtin->LLVMIntrinsic); | ||
| else if (Builtin->LLVMIntrinsic != 0) { | ||
| // Predicates must match the main datatype. | ||
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) | ||
| if (auto PredTy = dyn_cast<llvm::VectorType>(Ops[i]->getType())) | ||
| if (PredTy->getElementType()->isIntegerTy(1)) | ||
| Ops[i] = EmitSVEPredicateCast(Ops[i], getSVEType(TypeFlags)); | ||
|
|
||
| Function *F = CGM.getIntrinsic(Builtin->LLVMIntrinsic, | ||
| getSVEOverloadTypes(TypeFlags, Ty, Ops)); | ||
| Value *Call = Builder.CreateCall(F, Ops); | ||
| return Call; | ||
| } | ||
| // Should not happen! | ||
| if (Builtin->LLVMIntrinsic == 0) | ||
| return nullptr; | ||
|
|
||
| /// Should not happen | ||
| return nullptr; | ||
| // Predicates must match the main datatype. | ||
| for (unsigned i = 0, e = Ops.size(); i != e; ++i) | ||
| if (auto PredTy = dyn_cast<llvm::VectorType>(Ops[i]->getType())) | ||
| if (PredTy->getElementType()->isIntegerTy(1)) | ||
| Ops[i] = EmitSVEPredicateCast(Ops[i], getSVEType(TypeFlags)); | ||
|
|
||
| Function *F = | ||
| TypeFlags.isOverloadNone() | ||
| ? CGM.getIntrinsic(Builtin->LLVMIntrinsic) | ||
| : CGM.getIntrinsic(Builtin->LLVMIntrinsic, {getSVEType(TypeFlags)}); | ||
| Value *Call = Builder.CreateCall(F, Ops); | ||
|
|
||
| return FormSVEBuiltinResult(Call); | ||
| } | ||
|
|
||
| Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID, | ||
|
|
||
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.
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.
This is the same as
Ops.push_back(Arg);? And if so, you can combine it with the case below.