Skip to content

[Clang][SME2] Use tuple result of SME builtins directly. #109423

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 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 25 additions & 41 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9845,6 +9845,22 @@ Value *CodeGenFunction::EmitSVEPredicateCast(Value *Pred,
return C;
}

Value *CodeGenFunction::EmitSVEPredicateTupleCast(Value *PredTuple,
llvm::StructType *Ty) {
if (PredTuple->getType() == Ty)
return PredTuple;

Value *Ret = llvm::PoisonValue::get(Ty);
for (unsigned I = 0; I < Ty->getNumElements(); ++I) {
Value *Pred = Builder.CreateExtractValue(PredTuple, I);
Pred = EmitSVEPredicateCast(
Pred, cast<llvm::ScalableVectorType>(Ty->getTypeAtIndex(I)));
Ret = Builder.CreateInsertValue(Ret, Pred, I);
}

return Ret;
}

Value *CodeGenFunction::EmitSVEGatherLoad(const SVETypeFlags &TypeFlags,
SmallVectorImpl<Value *> &Ops,
unsigned IntID) {
Expand Down Expand Up @@ -10351,41 +10367,6 @@ Value *CodeGenFunction::EmitSVETupleCreate(const SVETypeFlags &TypeFlags,
return Tuple;
}

Value *CodeGenFunction::FormSVEBuiltinResult(Value *Call) {
// Multi-vector results should be broken up into a single (wide) result
// vector.
auto *StructTy = dyn_cast<StructType>(Call->getType());
if (!StructTy)
return Call;

auto *VTy = dyn_cast<ScalableVectorType>(StructTy->getTypeAtIndex(0U));
if (!VTy)
return Call;
unsigned N = StructTy->getNumElements();

// We may need to emit a cast to a svbool_t
bool IsPredTy = VTy->getElementType()->isIntegerTy(1);
unsigned MinElts = IsPredTy ? 16 : VTy->getMinNumElements();

ScalableVectorType *WideVTy =
ScalableVectorType::get(VTy->getElementType(), MinElts * N);
Value *Ret = llvm::PoisonValue::get(WideVTy);
for (unsigned I = 0; I < N; ++I) {
Value *SRet = Builder.CreateExtractValue(Call, I);
assert(SRet->getType() == VTy && "Unexpected type for result value");
Value *Idx = ConstantInt::get(CGM.Int64Ty, I * MinElts);

if (IsPredTy)
SRet = EmitSVEPredicateCast(
SRet, ScalableVectorType::get(Builder.getInt1Ty(), 16));

Ret = Builder.CreateInsertVector(WideVTy, Ret, SRet, Idx);
}
Call = Ret;

return Call;
}

void CodeGenFunction::GetAArch64SVEProcessedOperands(
unsigned BuiltinID, const CallExpr *E, SmallVectorImpl<Value *> &Ops,
SVETypeFlags TypeFlags) {
Expand Down Expand Up @@ -10516,12 +10497,16 @@ Value *CodeGenFunction::EmitAArch64SVEBuiltinExpr(unsigned BuiltinID,
getSVEOverloadTypes(TypeFlags, Ty, Ops));
Value *Call = Builder.CreateCall(F, Ops);

if (Call->getType() == Ty)
return Call;

// Predicate results must be converted to svbool_t.
if (auto PredTy = dyn_cast<llvm::VectorType>(Call->getType()))
if (PredTy->getScalarType()->isIntegerTy(1))
Call = EmitSVEPredicateCast(Call, cast<llvm::ScalableVectorType>(Ty));
if (auto PredTy = dyn_cast<llvm::ScalableVectorType>(Ty))
return EmitSVEPredicateCast(Call, PredTy);
if (auto PredTupleTy = dyn_cast<llvm::StructType>(Ty))
return EmitSVEPredicateTupleCast(Call, PredTupleTy);

return FormSVEBuiltinResult(Call);
llvm_unreachable("unsupported element count!");
}

switch (BuiltinID) {
Expand Down Expand Up @@ -10853,9 +10838,8 @@ Value *CodeGenFunction::EmitAArch64SMEBuiltinExpr(unsigned BuiltinID,
TypeFlags.isOverloadNone()
? CGM.getIntrinsic(Builtin->LLVMIntrinsic)
: CGM.getIntrinsic(Builtin->LLVMIntrinsic, {getSVEType(TypeFlags)});
Value *Call = Builder.CreateCall(F, Ops);

return FormSVEBuiltinResult(Call);
return Builder.CreateCall(F, Ops);
}

Value *CodeGenFunction::EmitAArch64BuiltinExpr(unsigned BuiltinID,
Expand Down
8 changes: 2 additions & 6 deletions clang/lib/CodeGen/CodeGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -4645,6 +4645,8 @@ class CodeGenFunction : public CodeGenTypeCache {
unsigned BuiltinID);
llvm::Value *EmitSVEPredicateCast(llvm::Value *Pred,
llvm::ScalableVectorType *VTy);
llvm::Value *EmitSVEPredicateTupleCast(llvm::Value *PredTuple,
llvm::StructType *Ty);
llvm::Value *EmitSVEGatherLoad(const SVETypeFlags &TypeFlags,
llvm::SmallVectorImpl<llvm::Value *> &Ops,
unsigned IntID);
Expand All @@ -4669,12 +4671,6 @@ class CodeGenFunction : public CodeGenTypeCache {
llvm::Value *EmitSVEStructStore(const SVETypeFlags &TypeFlags,
SmallVectorImpl<llvm::Value *> &Ops,
unsigned IntID);
/// FormSVEBuiltinResult - Returns the struct of scalable vectors as a wider
/// vector. It extracts the scalable vector from the struct and inserts into
/// the wider vector. This avoids the error when allocating space in llvm
/// for struct of scalable vectors if a function returns struct.
llvm::Value *FormSVEBuiltinResult(llvm::Value *Call);

llvm::Value *EmitAArch64SVEBuiltinExpr(unsigned BuiltinID, const CallExpr *E);

llvm::Value *EmitSMELd1St1(const SVETypeFlags &TypeFlags,
Expand Down
Loading
Loading