Skip to content

Commit cacabf0

Browse files
sdesmalen-armllvmbot
authored andcommitted
[Clang][AArch64] Emit 'unimplemented' diagnostic for SME (llvm#80295)
When a function F has ZA and ZT0 state, calls another function G that only shares ZT0 state with its caller, F will have to save ZA before the call to G, and restore it afterwards (rather than setting up a lazy-sve). This is not yet implemented in LLVM and does not result in a compile-time error either. So instead of silently generating incorrect code, it's better to emit an error saying this is not yet implemented. (cherry picked from commit 319f4c0)
1 parent 1cfd46f commit cacabf0

File tree

3 files changed

+40
-29
lines changed

3 files changed

+40
-29
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

+6
Original file line numberDiff line numberDiff line change
@@ -3711,6 +3711,12 @@ def err_sme_za_call_no_za_state : Error<
37113711
"call to a shared ZA function requires the caller to have ZA state">;
37123712
def err_sme_zt0_call_no_zt0_state : Error<
37133713
"call to a shared ZT0 function requires the caller to have ZT0 state">;
3714+
def err_sme_unimplemented_za_save_restore : Error<
3715+
"call to a function that shares state other than 'za' from a "
3716+
"function that has live 'za' state requires a spill/fill of ZA, which is not yet "
3717+
"implemented">;
3718+
def note_sme_use_preserves_za : Note<
3719+
"add '__arm_preserves(\"za\")' to the callee if it preserves ZA">;
37143720
def err_sme_definition_using_sm_in_non_sme_target : Error<
37153721
"function executed in streaming-SVE mode requires 'sme'">;
37163722
def err_sme_definition_using_za_in_non_sme_target : Error<

clang/lib/Sema/SemaChecking.cpp

+23-27
Original file line numberDiff line numberDiff line change
@@ -7545,47 +7545,43 @@ void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
75457545
}
75467546
}
75477547

7548-
// If the callee uses AArch64 SME ZA state but the caller doesn't define
7549-
// any, then this is an error.
7550-
FunctionType::ArmStateValue ArmZAState =
7548+
FunctionType::ArmStateValue CalleeArmZAState =
75517549
FunctionType::getArmZAState(ExtInfo.AArch64SMEAttributes);
7552-
if (ArmZAState != FunctionType::ARM_None) {
7550+
FunctionType::ArmStateValue CalleeArmZT0State =
7551+
FunctionType::getArmZT0State(ExtInfo.AArch64SMEAttributes);
7552+
if (CalleeArmZAState != FunctionType::ARM_None ||
7553+
CalleeArmZT0State != FunctionType::ARM_None) {
75537554
bool CallerHasZAState = false;
7555+
bool CallerHasZT0State = false;
75547556
if (const auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) {
75557557
auto *Attr = CallerFD->getAttr<ArmNewAttr>();
75567558
if (Attr && Attr->isNewZA())
75577559
CallerHasZAState = true;
7558-
else if (const auto *FPT =
7559-
CallerFD->getType()->getAs<FunctionProtoType>())
7560-
CallerHasZAState = FunctionType::getArmZAState(
7561-
FPT->getExtProtoInfo().AArch64SMEAttributes) !=
7562-
FunctionType::ARM_None;
7563-
}
7564-
7565-
if (!CallerHasZAState)
7566-
Diag(Loc, diag::err_sme_za_call_no_za_state);
7567-
}
7568-
7569-
// If the callee uses AArch64 SME ZT0 state but the caller doesn't define
7570-
// any, then this is an error.
7571-
FunctionType::ArmStateValue ArmZT0State =
7572-
FunctionType::getArmZT0State(ExtInfo.AArch64SMEAttributes);
7573-
if (ArmZT0State != FunctionType::ARM_None) {
7574-
bool CallerHasZT0State = false;
7575-
if (const auto *CallerFD = dyn_cast<FunctionDecl>(CurContext)) {
7576-
auto *Attr = CallerFD->getAttr<ArmNewAttr>();
75777560
if (Attr && Attr->isNewZT0())
75787561
CallerHasZT0State = true;
7579-
else if (const auto *FPT =
7580-
CallerFD->getType()->getAs<FunctionProtoType>())
7581-
CallerHasZT0State =
7562+
if (const auto *FPT = CallerFD->getType()->getAs<FunctionProtoType>()) {
7563+
CallerHasZAState |=
7564+
FunctionType::getArmZAState(
7565+
FPT->getExtProtoInfo().AArch64SMEAttributes) !=
7566+
FunctionType::ARM_None;
7567+
CallerHasZT0State |=
75827568
FunctionType::getArmZT0State(
75837569
FPT->getExtProtoInfo().AArch64SMEAttributes) !=
75847570
FunctionType::ARM_None;
7571+
}
75857572
}
75867573

7587-
if (!CallerHasZT0State)
7574+
if (CalleeArmZAState != FunctionType::ARM_None && !CallerHasZAState)
7575+
Diag(Loc, diag::err_sme_za_call_no_za_state);
7576+
7577+
if (CalleeArmZT0State != FunctionType::ARM_None && !CallerHasZT0State)
75887578
Diag(Loc, diag::err_sme_zt0_call_no_zt0_state);
7579+
7580+
if (CallerHasZAState && CalleeArmZAState == FunctionType::ARM_None &&
7581+
CalleeArmZT0State != FunctionType::ARM_None) {
7582+
Diag(Loc, diag::err_sme_unimplemented_za_save_restore);
7583+
Diag(Loc, diag::note_sme_use_preserves_za);
7584+
}
75897585
}
75907586
}
75917587

clang/test/Sema/aarch64-sme-func-attrs.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sme -fsyntax-only -verify %s
2-
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sme -fsyntax-only -verify=expected-cpp -x c++ %s
1+
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sme2 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -triple aarch64-none-linux-gnu -target-feature +sme2 -fsyntax-only -verify=expected-cpp -x c++ %s
33

44
// Valid attributes
55

@@ -445,3 +445,12 @@ void conflicting_state_attrs_preserves_out_zt0(void) __arm_preserves("zt0") __ar
445445
// expected-cpp-error@+2 {{conflicting attributes for state 'zt0'}}
446446
// expected-error@+1 {{conflicting attributes for state 'zt0'}}
447447
void conflicting_state_attrs_preserves_inout_zt0(void) __arm_preserves("zt0") __arm_inout("zt0");
448+
449+
// Test that we get a diagnostic for unimplemented case.
450+
void unimplemented_spill_fill_za(void (*share_zt0_only)(void) __arm_inout("zt0")) __arm_inout("za", "zt0") {
451+
// expected-cpp-error@+4 {{call to a function that shares state other than 'za' from a function that has live 'za' state requires a spill/fill of ZA, which is not yet implemented}}
452+
// expected-cpp-note@+3 {{add '__arm_preserves("za")' to the callee if it preserves ZA}}
453+
// expected-error@+2 {{call to a function that shares state other than 'za' from a function that has live 'za' state requires a spill/fill of ZA, which is not yet implemented}}
454+
// expected-note@+1 {{add '__arm_preserves("za")' to the callee if it preserves ZA}}
455+
share_zt0_only();
456+
}

0 commit comments

Comments
 (0)