[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin closed https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/amy-kwan approved this pull request. I don't have any further comments. LGTM. https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/lei137 approved this pull request. LGTM https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From 557e7163d744890aadfa703a81a0c4f2cd112517 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/8] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 15 +- clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 104 +++--- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 135 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 +- .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 ++ .../llvm/TargetParser/PPCTargetParser.def | 73 +- 9 files changed, 356 insertions(+), 39 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..1d488dff825ede 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,19 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, COMP_OP, \ +VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" @@ -906,7 +919,7 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const { llvm::Triple Triple = getTriple(); if (Triple.isOSAIX()) { -#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true) +#define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, OP, VALUE) .Case(NAME, true) return llvm::StringSwitch(CPUName) #include "llvm/TargetParser/PPCTargetParser.def" .Default(false); diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 9ee51ca7142c77..1565e4103d9ef8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16570,7 +16570,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16578,24 +16578,64 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -0,0 +1,43 @@ +// RUN: %clang_cc1 -triple powerpc-ibm-aix7.2.0.0 -emit-llvm -o - %s | FileCheck %s + +int main() { + int ret = 0; + ret += __builtin_cpu_supports("vsx"); + ret += __builtin_cpu_supports("htm"); + ret += __builtin_cpu_supports("cellbe"); + ret += __builtin_cpu_supports("power4"); + return ret; lei137 wrote: would be good to add a call that will directly return true, another call to syscfg and doc which is which within the src here. https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -141,46 +149,98 @@ PPC_LNX_CPU("power10",47) #define AIX_BUILTIN_PPC_TRUE 1 #define AIX_BUILTIN_PPC_FALSE 0 #define USE_SYS_CONF 2 - - // Supported COMPARE_OP values. - #define COMP_EQ 0 - + #define SYS_CALL 3 #endif // The value of SUPPORT_METHOD can be AIX_BUILTIN_PPC_TRUE, -// AIX_BUILTIN_PPC_FALSE, or USE_SYS_CONF. -// When the value of SUPPORT_METHOD is USE_SYS_CONF, the return value -// depends on the result of comparing the data member of -// _system_configuration specified by INDEX with a certain value. +// AIX_BUILTIN_PPC_FALSE, USE_SYS_CONF, SYS_CALL. +// When the value of SUPPORT_METHOD is set to USE_SYS_CONF, the return value +// depends on comparing VALUE with the specified data member of +// _system_configuration at INDEX, where the data member is masked by Mask. +// When the SUPPORT_METHOD value is set to SYS_CALL, the return value depends +// on comparing a VALUE with the return value of calling `getsystemcfg` +// with the parameter INDEX, which is then masked by Mask. +// AIX_BUILTIN_PPC_TRUE and AIX_BUILTIN_PPC_FALSE are for features +// that are supported or unsupported on all systems respectively. lei137 wrote: Maybe this will be more clear: ```suggestion // The value of SUPPORT_METHOD can be: //AIX_BUILTIN_PPC_TRUE : feature supported //AIX_BUILTIN_PPC_FALSE : feature not supported //USE_SYS_CONF : return value depends on comparing VALUE with the specified // data member of _system_configuration at INDEX, where the // data member is masked by Mask. //SYS_CALL : return value depends on comparing a VALUE with the return value // of calling `getsystemcfg` with the parameter INDEX, which is // then masked by Mask. ``` https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From 557e7163d744890aadfa703a81a0c4f2cd112517 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/7] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 15 +- clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 104 +++--- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 135 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 +- .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 ++ .../llvm/TargetParser/PPCTargetParser.def | 73 +- 9 files changed, 356 insertions(+), 39 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..1d488dff825ede 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,19 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, COMP_OP, \ +VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" @@ -906,7 +919,7 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const { llvm::Triple Triple = getTriple(); if (Triple.isOSAIX()) { -#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true) +#define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, OP, VALUE) .Case(NAME, true) return llvm::StringSwitch(CPUName) #include "llvm/TargetParser/PPCTargetParser.def" .Default(false); diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 9ee51ca7142c77..1565e4103d9ef8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16570,7 +16570,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16578,24 +16578,64 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From 557e7163d744890aadfa703a81a0c4f2cd112517 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/6] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 15 +- clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 104 +++--- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 135 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 +- .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 ++ .../llvm/TargetParser/PPCTargetParser.def | 73 +- 9 files changed, 356 insertions(+), 39 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..1d488dff825ede 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,19 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, COMP_OP, \ +VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" @@ -906,7 +919,7 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const { llvm::Triple Triple = getTriple(); if (Triple.isOSAIX()) { -#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true) +#define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, OP, VALUE) .Case(NAME, true) return llvm::StringSwitch(CPUName) #include "llvm/TargetParser/PPCTargetParser.def" .Default(false); diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 9ee51ca7142c77..1565e4103d9ef8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16570,7 +16570,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16578,24 +16578,64 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16570,32 +16570,53 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, CmpInst::Predicate CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = + CGM.CreateRuntimeFunction(FTy, "getsystemcfg"); + + FieldValue = + Builder.CreateCall(Func, {ConstantInt::get(Int32Ty, FieldIdx)}); +} +assert((FieldValue != nullptr) && amy-kwan wrote: I think we can probably just do `assert(FieldValue && "SupportMethod value is not defined in PPCTargetParser.def.");`? https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From 557e7163d744890aadfa703a81a0c4f2cd112517 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/5] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 15 +- clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 104 +++--- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 135 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 +- .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 ++ .../llvm/TargetParser/PPCTargetParser.def | 73 +- 9 files changed, 356 insertions(+), 39 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..1d488dff825ede 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,19 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, COMP_OP, \ +VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" @@ -906,7 +919,7 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const { llvm::Triple Triple = getTriple(); if (Triple.isOSAIX()) { -#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true) +#define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, OP, VALUE) .Case(NAME, true) return llvm::StringSwitch(CPUName) #include "llvm/TargetParser/PPCTargetParser.def" .Default(false); diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 9ee51ca7142c77..1565e4103d9ef8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16570,7 +16570,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16578,24 +16578,64 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -141,46 +149,98 @@ PPC_LNX_CPU("power10",47) #define AIX_BUILTIN_PPC_TRUE 1 #define AIX_BUILTIN_PPC_FALSE 0 #define USE_SYS_CONF 2 - - // Supported COMPARE_OP values. - #define COMP_EQ 0 - + #define SYS_CALL 3 #endif // The value of SUPPORT_METHOD can be AIX_BUILTIN_PPC_TRUE, -// AIX_BUILTIN_PPC_FALSE, or USE_SYS_CONF. -// When the value of SUPPORT_METHOD is USE_SYS_CONF, the return value -// depends on the result of comparing the data member of -// _system_configuration specified by INDEX with a certain value. +// AIX_BUILTIN_PPC_FALSE, USE_SYS_CONF, SYS_CALL. +// When the value of SUPPORT_METHOD is set to USE_SYS_CONF, the return value +// depends on comparing VALUE with the specified data member of +// _system_configuration at INDEX, where the data member is masked by Mask. +// When the SUPPORT_METHOD value is set to SYS_CALL, the return value depends +// on comparing a VALUE with the return value of calling `getsystemcfg` +// with the parameter INDEX, which is then masked by Mask. +// AIX_BUILTIN_PPC_TRUE and AIX_BUILTIN_PPC_FALSE are for features +// that are supported or unsupported on all systems respectively. #ifndef PPC_AIX_CPU #define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, COMPARE_OP, VALUE) #endif -// __builtin_cpu_is() is supported only on Power7 and up. -PPC_AIX_CPU("power4",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("ppc970",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("power5",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("power5+",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("power6",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("ppc-cell-be",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("power6x",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("ppca2",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("ppc405",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("ppc440",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("ppc464",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("ppc476",AIX_BUILTIN_PPC_FALSE,0,0,0) -PPC_AIX_CPU("power7",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC7_VALUE) -PPC_AIX_CPU("power8",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC8_VALUE) -PPC_AIX_CPU("power9",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC9_VALUE) -PPC_AIX_CPU("power10",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,COMP_EQ,AIX_PPC10_VALUE) +// __builtin_cpu_is() and __builtin_cpu_supports() are supported only on Power7 and up. +PPC_AIX_CPU("power4",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("ppc970",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("power5",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("power5+",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("power6",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("ppc-cell-be",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("power6x",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("ppca2",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("ppc405",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("ppc440",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("ppc464",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("ppc476",AIX_BUILTIN_PPC_FALSE,0,CmpInst::Predicate(),0) +PPC_AIX_CPU("power7",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,ICmpInst::ICMP_EQ,AIX_PPC7_VALUE) +PPC_AIX_CPU("power8",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,ICmpInst::ICMP_EQ,AIX_PPC8_VALUE) +PPC_AIX_CPU("power9",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,ICmpInst::ICMP_EQ,AIX_PPC9_VALUE) +PPC_AIX_CPU("power10",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,ICmpInst::ICMP_EQ,AIX_PPC10_VALUE) #undef PPC_AIX_CPU +#ifndef PPC_AIX_FEATURE +#define PPC_AIX_FEATURE(NAME,DESC,SUPPORT_METHOD,INDEX,MASK,COMPARE_OP,VALUE) +#endif + +PPC_AIX_FEATURE("4xxmac","4xx CPU has a Multiply Accumulator",AIX_BUILTIN_PPC_FALSE,0,0,CmpInst::Predicate(),0) +PPC_AIX_FEATURE("altivec","CPU has a SIMD/Vector Unit",USE_SYS_CONF,AIX_SYSCON_VMX_IDX,0,ICmpInst::ICMP_UGT,0) +PPC_AIX_FEATURE("arch_2_05","CPU supports ISA 205 (eg, POWER6)",AIX_BUILTIN_PPC_TRUE,0,0,CmpInst::Predicate(),0) +PPC_AIX_FEATURE("arch_2_06","CPU supports ISA 206 (eg, POWER7)",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,0,ICmpInst::ICMP_UGE,AIX_PPC7_VALUE) +PPC_AIX_FEATURE("arch_2_07","CPU supports ISA 207 (eg, POWER8)",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,0,ICmpInst::ICMP_UGE,AIX_PPC8_VALUE) +PPC_AIX_FEATURE("arch_3_00","CPU supports ISA 30 (eg, POWER9)", USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,0,ICmpInst::ICMP_UGE,AIX_PPC9_VALUE) +PPC_AIX_FEATURE("arch_3_1","CPU supports ISA 31 (eg, POWER10)", USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,0,ICmpInst::ICMP_UGE,AIX_PPC10_VALUE) +PPC_AIX_FEATURE("booke","CPU supports the Embedded ISA category",AIX_BUILTIN_PPC_FALSE,0,0,CmpInst::Predicate(),0) +PPC_AIX_FEATURE("cellbe","CPU has a CELL broadband engine",AIX_BUILTIN_PPC_FALSE,0,0,CmpInst::Predicate(),0) +PPC_AIX_FEATURE("darn","CPU supports the darn (deliver a random number) instruction",USE_SYS_CONF,AIX_SYSCON_IMPL_IDX,0,ICmpInst::ICMP_UGE,AIX_PPC10_VALUE)
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From 557e7163d744890aadfa703a81a0c4f2cd112517 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/4] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 15 +- clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 104 +++--- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 135 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 +- .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 ++ .../llvm/TargetParser/PPCTargetParser.def | 73 +- 9 files changed, 356 insertions(+), 39 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..1d488dff825ede 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,19 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, COMP_OP, \ +VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" @@ -906,7 +919,7 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const { llvm::Triple Triple = getTriple(); if (Triple.isOSAIX()) { -#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true) +#define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, OP, VALUE) .Case(NAME, true) return llvm::StringSwitch(CPUName) #include "llvm/TargetParser/PPCTargetParser.def" .Default(false); diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 9ee51ca7142c77..1565e4103d9ef8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16570,7 +16570,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16578,24 +16578,64 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { nemanjai wrote: My personal preference is to keep them separate as the added verbosity makes it very obvious what is being queried. https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -141,23 +149,30 @@ PPC_LNX_CPU("power10",47) #define AIX_BUILTIN_PPC_TRUE 1 #define AIX_BUILTIN_PPC_FALSE 0 #define USE_SYS_CONF 2 + #define SYS_CALL 3 // Supported COMPARE_OP values. #define COMP_EQ 0 + #define COMP_GT 1 + #define COMP_GE 2 + #define COMP_NE 3 nemanjai wrote: Can we not omit this and use `CmpInst::Predicate` in the function-style macros (thereby eliminating the need for the switch statement that uses them as well)? https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -0,0 +1,171 @@ +// RUN: echo "int main() { return __builtin_cpu_supports(\"4xxmac\");}" > %t.c nemanjai wrote: This is an interesting way of testing, where we create each test on the fly. I am not against it if it works on all platforms (including Windows). However, I would like to see one test case where we have multiple calls to the builtins, with multiple uses of each "support method" in the same compilation unit. https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16570,32 +16570,72 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, nemanjai wrote: This is now a very large lambda function AFAICT. Please extract it into a static function to aid readability. https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -141,23 +149,30 @@ PPC_LNX_CPU("power10",47) #define AIX_BUILTIN_PPC_TRUE 1 #define AIX_BUILTIN_PPC_FALSE 0 #define USE_SYS_CONF 2 + #define SYS_CALL 3 // Supported COMPARE_OP values. #define COMP_EQ 0 + #define COMP_GT 1 + #define COMP_GE 2 + #define COMP_NE 3 #endif // The value of SUPPORT_METHOD can be AIX_BUILTIN_PPC_TRUE, -// AIX_BUILTIN_PPC_FALSE, or USE_SYS_CONF. -// When the value of SUPPORT_METHOD is USE_SYS_CONF, the return value -// depends on the result of comparing the data member of -// _system_configuration specified by INDEX with a certain value. +// AIX_BUILTIN_PPC_FALSE, USE_SYS_CONF, SYS_CALL. +// When the value of SUPPORT_METHOD is set to USE_SYS_CONF, the return value +// depends on comparing VALUE with the specified data member of +// _system_configuration at INDEX, where the data member is masked by Mask. +// When the SUPPORT_METHOD value is set to SYS_CALL, the return value depends +// on comparing a VALUE with the return value of calling `getsystemcfg` +// with the parameter INDEX, which is then masked by Mask. nemanjai wrote: And presumably: ``` // AIX_BUILTIN_PPC_TRUE and AIX_BUILTIN_PPC_FALSE are for features // that are supported or unsupported on all systems respectively. ``` ? https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From 557e7163d744890aadfa703a81a0c4f2cd112517 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/2] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 15 +- clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 104 +++--- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 135 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 +- .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 ++ .../llvm/TargetParser/PPCTargetParser.def | 73 +- 9 files changed, 356 insertions(+), 39 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..1d488dff825ede 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,19 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, COMP_OP, \ +VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" @@ -906,7 +919,7 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const { llvm::Triple Triple = getTriple(); if (Triple.isOSAIX()) { -#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true) +#define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, OP, VALUE) .Case(NAME, true) return llvm::StringSwitch(CPUName) #include "llvm/TargetParser/PPCTargetParser.def" .Default(false); diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 9ee51ca7142c77..1565e4103d9ef8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16570,7 +16570,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16578,24 +16578,64 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From 557e7163d744890aadfa703a81a0c4f2cd112517 Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 15 +- clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 104 +++--- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 135 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 +- .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 ++ .../llvm/TargetParser/PPCTargetParser.def | 73 +- 9 files changed, 356 insertions(+), 39 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..1d488dff825ede 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,19 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, COMP_OP, \ +VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" @@ -906,7 +919,7 @@ bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { bool PPCTargetInfo::validateCpuIs(StringRef CPUName) const { llvm::Triple Triple = getTriple(); if (Triple.isOSAIX()) { -#define PPC_AIX_CPU(NAME, SUPPORT, INDEX, OP, VALUE) .Case(NAME, true) +#define PPC_AIX_CPU(NAME, SUPPORT_METHOD, INDEX, OP, VALUE) .Case(NAME, true) return llvm::StringSwitch(CPUName) #include "llvm/TargetParser/PPCTargetParser.def" .Default(false); diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 9ee51ca7142c77..1565e4103d9ef8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16570,7 +16570,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16578,24 +16578,64 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/8] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = +
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16560,32 +16560,72 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = + CGM.CreateRuntimeFunction(FTy, "getsystemcfg"); + + FieldValue = + Builder.CreateCall(Func, {ConstantInt::get(Int32Ty, FieldIdx)}); +} +assert((FieldValue != nullptr) && + "unsupported SupportMethod defined in the PPCTargetParser.def."); amy-kwan wrote: ```suggestion "SupportMethod value is not defined in PPCTargetParser.def."); ``` https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { amy-kwan wrote: This sounds reasonable to me, thanks! https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { diggerlin wrote: > Does this need to be a separate function if it is the same as > `supportsCpuIs()`? Can they be combined? yes, we can combined them , but it need another NFC to do it. since not only PPC.h has the two function but also X86.h https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/7] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = +
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16560,32 +16560,72 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = + CGM.CreateRuntimeFunction(FTy, "getsystemcfg"); + + FieldValue = + Builder.CreateCall(Func, {ConstantInt::get(Int32Ty, FieldIdx)}); +} +assert((FieldValue != nullptr) && + "unsupported SupportMethod defined in the PPCTargetParser.def."); + +if (Mask) + FieldValue = Builder.CreateAnd(FieldValue, Mask); -llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); -llvm::Constant *SysConf = -CGM.CreateRuntimeVariable(STy, "_system_configuration"); +CmpInst::Predicate PreOp; +switch (CompOp) { +case COMP_EQ: + PreOp = ICmpInst::ICMP_EQ; + break; +case COMP_GT: + PreOp = ICmpInst::ICMP_UGT; + break; +case COMP_GE: + PreOp = ICmpInst::ICMP_UGE; + break; +case COMP_NE: + PreOp = ICmpInst::ICMP_NE; + break; +default: + llvm_unreachable( + "Compare type does not match types defined in PPCTargetParser.def!"); +} -// Grab the appropriate field from _system_configuration. -llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), - ConstantInt::get(Int32Ty, FieldIdx)}; +llvm::Type *ValueType = FieldValue->getType(); +assert( +(ValueType->isIntegerTy(64) || ValueType->isIntegerTy(32)) && amy-kwan wrote: We can pull out `ValueType->isIntegerTy(64)` since we use it twice (here and below). https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { amy-kwan wrote: Does this need to be a separate function if it is the same as `supportsCpuIs()`? Can they be combined? https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/amy-kwan commented: Group review comments. https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -132,6 +132,14 @@ PPC_LNX_CPU("power10",47) #ifndef AIX_POWERPC_USE_SYS_CONF #define AIX_POWERPC_USE_SYS_CONF #define AIX_SYSCON_IMPL_IDX 1 + #define AIX_SYSCON_CACHE_IDX 5 + #define AIX_SYSCON_SMT_IDX 44 + #define AIX_SYSCON_VMX_IDX 46 + #define AIX_SYSCON_DFP_IDX 53 + + #define SC_TM_VER 59 + #define SC_MMA_VER 62 amy-kwan wrote: ```suggestion #define SYC_CALL_TM_VER 59 #define SYS_CALL_MMA_VER 62 ``` It would be more clear to expand what `SC` is, because it can mean either sys call or sys conf. https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/amy-kwan edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/6] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = +
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16560,32 +16560,69 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = + CGM.CreateRuntimeFunction(FTy, "getsystemcfg"); + + FieldValue = + Builder.CreateCall(Func, {ConstantInt::get(Int32Ty, FieldIdx)}); +} + +if (Mask) + FieldValue = Builder.CreateAnd(FieldValue, Mask); -llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); -llvm::Constant *SysConf = -CGM.CreateRuntimeVariable(STy, "_system_configuration"); +CmpInst::Predicate PreOp; +switch (CompOp) { +case COMP_EQ: + PreOp = ICmpInst::ICMP_EQ; + break; +case COMP_GT: + PreOp = ICmpInst::ICMP_UGT; + break; +case COMP_GE: + PreOp = ICmpInst::ICMP_UGE; + break; +case COMP_NE: + PreOp = ICmpInst::ICMP_NE; + break; +default: + llvm_unreachable("Compare type is not correct in PPCTargetParser.def."); amy-kwan wrote: Maybe we can say, "Compare type does not match types defined in PPCTargetParser.def!" https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16560,32 +16560,69 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = + CGM.CreateRuntimeFunction(FTy, "getsystemcfg"); + + FieldValue = + Builder.CreateCall(Func, {ConstantInt::get(Int32Ty, FieldIdx)}); +} + +if (Mask) + FieldValue = Builder.CreateAnd(FieldValue, Mask); -llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); -llvm::Constant *SysConf = -CGM.CreateRuntimeVariable(STy, "_system_configuration"); +CmpInst::Predicate PreOp; +switch (CompOp) { +case COMP_EQ: + PreOp = ICmpInst::ICMP_EQ; + break; +case COMP_GT: + PreOp = ICmpInst::ICMP_UGT; + break; +case COMP_GE: + PreOp = ICmpInst::ICMP_UGE; + break; +case COMP_NE: + PreOp = ICmpInst::ICMP_NE; + break; +default: + llvm_unreachable("Compare type is not correct in PPCTargetParser.def."); +} -// Grab the appropriate field from _system_configuration. -llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), - ConstantInt::get(Int32Ty, FieldIdx)}; +llvm::Type *ValueType = FieldValue->getType(); diggerlin wrote: I will add assert. thanks https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/5] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = +
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -141,23 +149,30 @@ PPC_LNX_CPU("power10",47) #define AIX_BUILTIN_PPC_TRUE 1 #define AIX_BUILTIN_PPC_FALSE 0 #define USE_SYS_CONF 2 + #define SYS_CALL 3 // Supported COMPARE_OP values. #define COMP_EQ 0 + #define COMP_GT 1 + #define COMP_GE 2 + #define COMP_NE 3 #endif // The value of SUPPORT_METHOD can be AIX_BUILTIN_PPC_TRUE, -// AIX_BUILTIN_PPC_FALSE, or USE_SYS_CONF. -// When the value of SUPPORT_METHOD is USE_SYS_CONF, the return value -// depends on the result of comparing the data member of -// _system_configuration specified by INDEX with a certain value. +// AIX_BUILTIN_PPC_FALSE, USE_SYS_CONF, API_CALL. amy-kwan wrote: ```suggestion // AIX_BUILTIN_PPC_FALSE, USE_SYS_CONF, or SYS_CALL. ``` I don't see an API_CALL unless I am missing something. Do we mean `SYS_CALL`? https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16560,32 +16560,69 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = + CGM.CreateRuntimeFunction(FTy, "getsystemcfg"); + + FieldValue = + Builder.CreateCall(Func, {ConstantInt::get(Int32Ty, FieldIdx)}); +} + +if (Mask) + FieldValue = Builder.CreateAnd(FieldValue, Mask); -llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); -llvm::Constant *SysConf = -CGM.CreateRuntimeVariable(STy, "_system_configuration"); +CmpInst::Predicate PreOp; +switch (CompOp) { +case COMP_EQ: + PreOp = ICmpInst::ICMP_EQ; + break; +case COMP_GT: + PreOp = ICmpInst::ICMP_UGT; + break; +case COMP_GE: + PreOp = ICmpInst::ICMP_UGE; + break; +case COMP_NE: + PreOp = ICmpInst::ICMP_NE; + break; +default: + llvm_unreachable("Compare type is not correct in PPCTargetParser.def."); +} -// Grab the appropriate field from _system_configuration. -llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), - ConstantInt::get(Int32Ty, FieldIdx)}; +llvm::Type *ValueType = FieldValue->getType(); amy-kwan wrote: Might be a dumb question, but `FieldValue` is initially set to `nullptr` initially and only gets set in two separate conditions. I assume we expect `FieldValue` is be non-null at this point, and if so, is something like an `assert()` needed here? https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
@@ -16560,32 +16560,69 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = + CGM.CreateRuntimeFunction(FTy, "getsystemcfg"); + + FieldValue = + Builder.CreateCall(Func, {ConstantInt::get(Int32Ty, FieldIdx)}); +} + +if (Mask) + FieldValue = Builder.CreateAnd(FieldValue, Mask); -llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); -llvm::Constant *SysConf = -CGM.CreateRuntimeVariable(STy, "_system_configuration"); +CmpInst::Predicate PreOp; +switch (CompOp) { +case COMP_EQ: + PreOp = ICmpInst::ICMP_EQ; + break; +case COMP_GT: + PreOp = ICmpInst::ICMP_UGT; + break; +case COMP_GE: + PreOp = ICmpInst::ICMP_UGE; + break; +case COMP_NE: + PreOp = ICmpInst::ICMP_NE; + break; +default: + llvm_unreachable("Compare type is not correct in PPCTargetParser.def."); amy-kwan wrote: This message might be a little bit confusing. Do we mean that the compare types do not match the ones defined within `PPCTargetParser.def`? https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/4] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = +
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin edited https://github.com/llvm/llvm-project/pull/82809 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/3] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = +
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin updated https://github.com/llvm/llvm-project/pull/82809 >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH 1/2] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue = Builder.CreateAlignedLoad(Int32Ty, FieldValue, + CharUnits::fromQuantity(4)); +} else if (SupportMethod == SYS_CALL) { + llvm::FunctionType *FTy = + llvm::FunctionType::get(Int64Ty, Int32Ty, false); + llvm::FunctionCallee Func = +
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
llvmbot wrote: @llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-backend-powerpc Author: zhijian lin (diggerlin) Changes The features which __builtin_cpu_support() supports are descripted in https://gcc.gnu.org/onlinedocs/gcc-8.1.0/gcc/PowerPC-Built-in-Functions.html#:~:text=The%20__builtin_cpu_supports%20function%20requires,in%20function%20is%20fully%20supported. the PR implements a subset of features of function __builtin_cpu_support() for AIX OS based on the information which AIX kernel runtime variable _system_configuration.version of /usr/include/sys/systemcfg.h and getsystemcfg() can provide. --- Patch is 28.81 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/82809.diff 9 Files Affected: - (modified) clang/lib/Basic/Targets/PPC.cpp (+12) - (modified) clang/lib/Basic/Targets/PPC.h (+8-1) - (modified) clang/lib/CodeGen/CGBuiltin.cpp (+77-22) - (modified) clang/lib/Sema/SemaChecking.cpp (+2-4) - (modified) clang/test/CodeGen/aix-builtin-cpu-is.c (+2-2) - (added) clang/test/CodeGen/aix-builtin-cpu-supports.c (+154) - (modified) clang/test/Sema/aix-builtin-cpu-unsupports.c (+38-2) - (added) clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c (+9) - (modified) llvm/include/llvm/TargetParser/PPCTargetParser.def (+67-6) ``diff diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] = {ConstantInt::get(Int32Ty, 0), + ConstantInt::get(Int32Ty, FieldIdx)}; + + FieldValue = Builder.CreateGEP(STy, SysConf, Idxs); + FieldValue =
[clang] [llvm] Implement a subset of builtin_cpu_supports() features (PR #82809)
https://github.com/diggerlin created https://github.com/llvm/llvm-project/pull/82809 The features which __builtin_cpu_support() supports are descripted in https://gcc.gnu.org/onlinedocs/gcc-8.1.0/gcc/PowerPC-Built-in-Functions.html#:~:text=The%20__builtin_cpu_supports%20function%20requires,in%20function%20is%20fully%20supported. the PR implements a subset of features of function __builtin_cpu_support() for AIX OS based on the information which AIX kernel runtime variable _system_configuration.version of /usr/include/sys/systemcfg.h and getsystemcfg() can provide. >From cef79b36bcb3f4b7452d01aafdf111ff0e50605d Mon Sep 17 00:00:00 2001 From: zhijian Date: Fri, 23 Feb 2024 13:23:18 -0500 Subject: [PATCH] Implement a subset of builtin_cpu_supports() features --- clang/lib/Basic/Targets/PPC.cpp | 12 ++ clang/lib/Basic/Targets/PPC.h | 9 +- clang/lib/CodeGen/CGBuiltin.cpp | 99 --- clang/lib/Sema/SemaChecking.cpp | 6 +- clang/test/CodeGen/aix-builtin-cpu-is.c | 4 +- clang/test/CodeGen/aix-builtin-cpu-supports.c | 154 ++ clang/test/Sema/aix-builtin-cpu-unsupports.c | 40 - .../test/Sema/builtin-cpu-unsupports-AIX-Os.c | 9 + .../llvm/TargetParser/PPCTargetParser.def | 73 - 9 files changed, 369 insertions(+), 37 deletions(-) create mode 100644 clang/test/CodeGen/aix-builtin-cpu-supports.c create mode 100644 clang/test/Sema/builtin-cpu-unsupports-AIX-Os.c diff --git a/clang/lib/Basic/Targets/PPC.cpp b/clang/lib/Basic/Targets/PPC.cpp index aebe51bfa4daad..17b462c73bfc6a 100644 --- a/clang/lib/Basic/Targets/PPC.cpp +++ b/clang/lib/Basic/Targets/PPC.cpp @@ -897,6 +897,18 @@ ArrayRef PPCTargetInfo::getTargetBuiltins() const { } bool PPCTargetInfo::validateCpuSupports(StringRef FeatureStr) const { + llvm::Triple Triple = getTriple(); + if (Triple.isOSAIX()) { +#define PPC_AIX_FEATURE(NAME, DESC, SUPPORT_METHOD, INDEX, MASK, OP, VALUE) \ + .Case(NAME, true) +return llvm::StringSwitch(FeatureStr) +#include "llvm/TargetParser/PPCTargetParser.def" +.Default(false); + } + + assert(Triple.isOSLinux() && + "__builtin_cpu_supports() is only supported for AIX and Linux."); + #define PPC_LNX_FEATURE(NAME, DESC, ENUMNAME, ENUMVAL, HWCAPN) .Case(NAME, true) return llvm::StringSwitch(FeatureStr) #include "llvm/TargetParser/PPCTargetParser.def" diff --git a/clang/lib/Basic/Targets/PPC.h b/clang/lib/Basic/Targets/PPC.h index 70683916a8b04f..39b52d362f36b8 100644 --- a/clang/lib/Basic/Targets/PPC.h +++ b/clang/lib/Basic/Targets/PPC.h @@ -364,7 +364,14 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo { // have Glibc since it is Glibc that provides the HWCAP[2] in the auxv. static constexpr int MINIMUM_AIX_OS_MAJOR = 7; static constexpr int MINIMUM_AIX_OS_MINOR = 2; - bool supportsCpuSupports() const override { return getTriple().isOSGlibc(); } + bool supportsCpuSupports() const override { +llvm::Triple Triple = getTriple(); +// AIX 7.2 is the minimum requirement to support __builtin_cpu_supports(). +return Triple.isOSGlibc() || + (Triple.isOSAIX() && +!Triple.isOSVersionLT(MINIMUM_AIX_OS_MAJOR, MINIMUM_AIX_OS_MINOR)); + } + bool supportsCpuIs() const override { llvm::Triple Triple = getTriple(); // AIX 7.2 is the minimum requirement to support __builtin_cpu_is(). diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 734eb5a035ca49..cf4ddc52ff362a 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -16560,7 +16560,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, #include "llvm/TargetParser/PPCTargetParser.def" auto GenAIXPPCBuiltinCpuExpr = [&](unsigned SupportMethod, unsigned FieldIdx, - unsigned CompOp, + unsigned Mask, unsigned CompOp, unsigned OpValue) -> Value * { if (SupportMethod == AIX_BUILTIN_PPC_FALSE) return llvm::ConstantInt::getFalse(ConvertType(E->getType())); @@ -16568,24 +16568,61 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID, if (SupportMethod == AIX_BUILTIN_PPC_TRUE) return llvm::ConstantInt::getTrue(ConvertType(E->getType())); -assert(SupportMethod <= USE_SYS_CONF && "Invalid value for SupportMethod."); -assert((CompOp == COMP_EQ) && "Only equal comparisons are supported."); +assert(SupportMethod <= SYS_CALL && "Invalid value for SupportMethod."); + +llvm::Value *FieldValue = nullptr; +if (SupportMethod == USE_SYS_CONF) { + llvm::Type *STy = llvm::StructType::get(PPC_SYSTEMCONFIG_TYPE); + llvm::Constant *SysConf = + CGM.CreateRuntimeVariable(STy, "_system_configuration"); + + // Grab the appropriate field from _system_configuration. + llvm::Value *Idxs[] =