https://gcc.gnu.org/g:739d9f765577dad9e1b2c16052360076e5d6efbc
commit r16-8409-g739d9f765577dad9e1b2c16052360076e5d6efbc Author: Lulu Cheng <[email protected]> Date: Tue Mar 24 11:32:25 2026 +0800 LoongArch: Implement TARGET_CHECK_TARGET_CLONE_VERSION. This is to be able to ignore invalid versions to allow some portability of code using target_clones attributes. Add warning tests for invalid target_clones versions. gcc/ChangeLog: * config/loongarch/loongarch-protos.h (loongarch_parse_fmv_features): Modify parameter type. * config/loongarch/loongarch-target-attr.cc (loongarch_parse_fmv_features): Some errors have been changed to warnings. * config/loongarch/loongarch.cc (loongarch_process_target_version_attr): Update parameters. (loongarch_check_target_clone_version): Likewise. (loongarch_option_same_function_versions): Likewise. (TARGET_CHECK_TARGET_CLONE_VERSION): Define. gcc/testsuite/ChangeLog: * gcc.target/loongarch/attr-check-error-message8.c: Removed. * gcc.target/loongarch/attr-check-error-message9.c: Removed. * gcc.target/loongarch/attr-check-warning-message1.c: New test. * gcc.target/loongarch/attr-check-warning-message2.c: New test. * gcc.target/loongarch/attr-check-warning-message3.c: New test. Diff: --- gcc/config/loongarch/loongarch-protos.h | 3 ++- gcc/config/loongarch/loongarch-target-attr.cc | 27 +++++++++++---------- gcc/config/loongarch/loongarch.cc | 28 ++++++++++++++++++---- .../loongarch/attr-check-error-message8.c | 6 ----- .../loongarch/attr-check-error-message9.c | 6 ----- .../loongarch/attr-check-warning-message1.c | 7 ++++++ .../loongarch/attr-check-warning-message2.c | 7 ++++++ .../loongarch/attr-check-warning-message3.c | 7 ++++++ 8 files changed, 61 insertions(+), 30 deletions(-) diff --git a/gcc/config/loongarch/loongarch-protos.h b/gcc/config/loongarch/loongarch-protos.h index e0c449274a20..11575a15454c 100644 --- a/gcc/config/loongarch/loongarch-protos.h +++ b/gcc/config/loongarch/loongarch-protos.h @@ -229,7 +229,8 @@ extern void loongarch_register_pragmas (void); extern bool loongarch_process_target_attr (tree args, tree fndecl); extern rtx loongarch_gen_stepped_int_parallel (unsigned int nelts, int base, int step); -extern bool loongarch_parse_fmv_features (tree, string_slice, loongarch_fmv_feature_mask *, +extern bool loongarch_parse_fmv_features (location_t, string_slice, + loongarch_fmv_feature_mask *, auto_vec<unsigned int> *); extern void get_feature_mask_for_version (tree, loongarch_fmv_feature_mask *, auto_vec<unsigned int> *); diff --git a/gcc/config/loongarch/loongarch-target-attr.cc b/gcc/config/loongarch/loongarch-target-attr.cc index 13ad2ed37313..99e5a3e22145 100644 --- a/gcc/config/loongarch/loongarch-target-attr.cc +++ b/gcc/config/loongarch/loongarch-target-attr.cc @@ -524,13 +524,10 @@ loongarch_option_valid_attribute_p (tree fndecl, tree, tree args, int) priority calculated by the feature string. */ bool -loongarch_parse_fmv_features (tree decl, string_slice str, +loongarch_parse_fmv_features (location_t loc, string_slice str, loongarch_fmv_feature_mask *feature_mask, auto_vec<unsigned int> *feature_priority) { - location_t loc - = decl == NULL ? UNKNOWN_LOCATION : DECL_SOURCE_LOCATION (decl); - if (feature_mask) *feature_mask = 0; @@ -625,7 +622,7 @@ loongarch_parse_fmv_features (tree decl, string_slice str, { string_slice arch_name = attr_str; string_slice::tokenize (&arch_name, "="); - if (!arch_name.is_valid ()) + if (arch_name.empty ()) { error_at (loc, "in attribute %qs you need to set a legal value " "for \"arch\"", attr_str.begin ()); @@ -659,8 +656,10 @@ loongarch_parse_fmv_features (tree decl, string_slice str, } else { - error_at (loc, "in attribute %qs you need to set a legal value " - "for \"arch\"", attr_str.begin ()); + if (loc != UNKNOWN_LOCATION) + warning_at (loc, OPT_Wattributes, + "in attribute %qs you need to set a legal value " + "for \"arch\"", attr_str.begin ()); return false; } @@ -681,9 +680,11 @@ loongarch_parse_fmv_features (tree decl, string_slice str, { if (loongarch_attributes[i].feat_mask == 0) { - error_at (loc, "attribute %qs is not supported in " - "%<target_version%> or %<target_clones%>", - attr_str.begin ()); + if (loc != UNKNOWN_LOCATION) + warning_at (loc, OPT_Wattributes, + "attribute %qs is not supported in " + "%<target_version%> or %<target_clones%>", + attr_str.begin ()); return false; } @@ -698,8 +699,10 @@ loongarch_parse_fmv_features (tree decl, string_slice str, if (i == num_features - 1) { - error_at (loc, "%qs is not supported in target attribute", - attr_str.begin ()); + if (loc != UNKNOWN_LOCATION) + warning_at (loc, OPT_Wattributes, + "%qs is not supported in target attribute", + attr_str.begin ()); return false; } } diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index 37ef30ec6d98..33e728ddf5a6 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -11507,7 +11507,7 @@ loongarch_process_target_version_attr (tree args, tree fndecl) if (str == "default") return true; - if (loongarch_parse_fmv_features (fndecl, str, NULL, NULL) == false) + if (loongarch_parse_fmv_features (loc, str, NULL, NULL) == false) return false; /* Get the attribute string and take out only the option part. @@ -11729,8 +11729,22 @@ get_feature_mask_for_version (tree decl, string_slice version_string = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (version_attr))); - loongarch_parse_fmv_features (decl, version_string, feature_mask, - feature_priority); + loongarch_parse_fmv_features (DECL_SOURCE_LOCATION (decl), version_string, + feature_mask, feature_priority); +} + +/* Implement TARGET_CHECK_TARGET_CLONE_VERSION. */ + +bool +loongarch_check_target_clone_version (string_slice str, location_t *loc) +{ + str = str.strip (); + + if (str == "default") + return true; + + return loongarch_parse_fmv_features (loc == NULL ? UNKNOWN_LOCATION : *loc, + str, NULL, NULL); } /* This adds a condition to the basic_block NEW_BB in function FUNCTION_DECL @@ -12088,9 +12102,9 @@ loongarch_option_same_function_versions (string_slice str1, const_tree, { loongarch_fmv_feature_mask feature_mask1; loongarch_fmv_feature_mask feature_mask2; - loongarch_parse_fmv_features (NULL, str1, + loongarch_parse_fmv_features (UNKNOWN_LOCATION, str1, &feature_mask1, NULL); - loongarch_parse_fmv_features (NULL, str2, + loongarch_parse_fmv_features (UNKNOWN_LOCATION, str2, &feature_mask2, NULL); return feature_mask1 == feature_mask2; @@ -12395,6 +12409,10 @@ loongarch_option_same_function_versions (string_slice str1, const_tree, #define TARGET_MANGLE_DECL_ASSEMBLER_NAME \ loongarch_mangle_decl_assembler_name +#undef TARGET_CHECK_TARGET_CLONE_VERSION +#define TARGET_CHECK_TARGET_CLONE_VERSION \ + loongarch_check_target_clone_version + #undef TARGET_GENERATE_VERSION_DISPATCHER_BODY #define TARGET_GENERATE_VERSION_DISPATCHER_BODY \ loongarch_generate_version_dispatcher_body diff --git a/gcc/testsuite/gcc.target/loongarch/attr-check-error-message8.c b/gcc/testsuite/gcc.target/loongarch/attr-check-error-message8.c deleted file mode 100644 index f27712bf5df9..000000000000 --- a/gcc/testsuite/gcc.target/loongarch/attr-check-error-message8.c +++ /dev/null @@ -1,6 +0,0 @@ -/* { dg-do compile } */ -/* { dg-options "-O2 -Wno-attributes" } */ - -__attribute__ ((target_clones ("default","cmodel=normal"))) void -test (void) /* { dg-error "attribute \\\'cmodel=normal\\\' is not supported in \\\'target_version\\\' or \\\'target_clones\\\'" } */ -{} diff --git a/gcc/testsuite/gcc.target/loongarch/attr-check-error-message9.c b/gcc/testsuite/gcc.target/loongarch/attr-check-error-message9.c deleted file mode 100644 index 4e02579942fd..000000000000 --- a/gcc/testsuite/gcc.target/loongarch/attr-check-error-message9.c +++ /dev/null @@ -1,6 +0,0 @@ -/* { dg-do compile } */ -/* { dg-options "-O2 -Wno-attributes" } */ - -__attribute__ ((target_clones ("default","123456"))) void -test (void) /* { dg-error "\\\'123456\\\' is not supported in target attribute" } */ -{} diff --git a/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message1.c b/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message1.c new file mode 100644 index 000000000000..a5da86a4f59e --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message1.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-require-ifunc "" } */ +/* { dg-options "-O0" } */ + +__attribute__ ((target_clones ("default","123456"))) void +test (void) /* { dg-warning "\\\'123456\\\' is not supported in target attribute" } */ +{} diff --git a/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message2.c b/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message2.c new file mode 100644 index 000000000000..117beddc6196 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message2.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-require-ifunc "" } */ +/* { dg-options "-O0" } */ + +__attribute__ ((target_clones ("default","arch=123456"))) void +test (void) /* { dg-warning "in attribute \\\'arch=123456\\\' you need to set a legal value for \\\"arch\\\"" } */ +{} diff --git a/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message3.c b/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message3.c new file mode 100644 index 000000000000..f1339f9fea69 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/attr-check-warning-message3.c @@ -0,0 +1,7 @@ +/* { dg-do compile } */ +/* { dg-require-ifunc "" } */ +/* { dg-options "-O0" } */ + +__attribute__ ((target_clones ("default","cmodel=normal"))) void +test (void) /* { dg-warning "attribute \\\'cmodel=normal\\\' is not supported in \\\'target_version\\\' or \\\'target_clones\\\'" } */ +{}
