Index: clang/include/clang/Basic/TargetInfo.h =================================================================== --- clang/include/clang/Basic/TargetInfo.h +++ clang/include/clang/Basic/TargetInfo.h @@ -225,7 +225,6 @@ bool HasStrictFP; unsigned char MaxAtomicPromoteWidth, MaxAtomicInlineWidth; - unsigned short SimdDefaultAlign; std::string DataLayoutString; const char *UserLabelPrefix; const char *MCountName; @@ -791,10 +790,6 @@ /// Return the maximum vector alignment supported for the given target. unsigned getMaxVectorAlign() const { return MaxVectorAlign; } - /// Return default simd alignment for the given target. Generally, this - /// value is type-specific, but this alignment can be used for most of the - /// types for the given target. - unsigned getSimdDefaultAlign() const { return SimdDefaultAlign; } unsigned getMaxOpenCLWorkGroupSize() const { return MaxOpenCLWorkGroupSize; } Index: clang/lib/AST/ASTContext.cpp =================================================================== --- clang/lib/AST/ASTContext.cpp +++ clang/lib/AST/ASTContext.cpp @@ -80,6 +80,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Triple.h" +#include "llvm/Frontend/OpenMP/OMPIRBuilder.h" #include "llvm/Support/Capacity.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Compiler.h" @@ -94,6 +95,7 @@ #include #include #include +#include #include #include #include @@ -2496,7 +2498,16 @@ } unsigned ASTContext::getOpenMPDefaultSimdAlign(QualType T) const { - unsigned SimdAlign = getTargetInfo().getSimdDefaultAlign(); + const std::vector &TargetFeatures = + Target->getTargetOpts().Features; + std::string TargetFeaturesString = std::accumulate( + TargetFeatures.cbegin(), TargetFeatures.cend(), std::string(), + [](const std::string &s1, const std::string &s2) { + return s1.empty() ? s2 : s1 + "," + s2; + }); + unsigned SimdAlign = llvm::OpenMPIRBuilder ::getVectorTypeAlignment( + Target->getTriple().str(), Target->getTargetOpts().CPU, + TargetFeaturesString); return SimdAlign; } Index: clang/lib/Basic/TargetInfo.cpp =================================================================== --- clang/lib/Basic/TargetInfo.cpp +++ clang/lib/Basic/TargetInfo.cpp @@ -95,7 +95,6 @@ MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 0; MaxVectorAlign = 0; MaxTLSAlign = 0; - SimdDefaultAlign = 0; SizeType = UnsignedLong; PtrDiffType = SignedLong; IntMaxType = SignedLongLong; Index: clang/lib/Basic/Targets/PPC.h =================================================================== --- clang/lib/Basic/Targets/PPC.h +++ clang/lib/Basic/Targets/PPC.h @@ -88,7 +88,6 @@ PPCTargetInfo(const llvm::Triple &Triple, const TargetOptions &) : TargetInfo(Triple) { SuitableAlign = 128; - SimdDefaultAlign = 128; LongDoubleWidth = LongDoubleAlign = 128; LongDoubleFormat = &llvm::APFloat::PPCDoubleDouble(); HasStrictFP = true; Index: clang/lib/Basic/Targets/WebAssembly.h =================================================================== --- clang/lib/Basic/Targets/WebAssembly.h +++ clang/lib/Basic/Targets/WebAssembly.h @@ -50,7 +50,6 @@ SuitableAlign = 128; LargeArrayMinWidth = 128; LargeArrayAlign = 128; - SimdDefaultAlign = 128; SigAtomicType = SignedLong; LongDoubleWidth = LongDoubleAlign = 128; LongDoubleFormat = &llvm::APFloat::IEEEquad(); Index: clang/lib/Basic/Targets/X86.cpp =================================================================== --- clang/lib/Basic/Targets/X86.cpp +++ clang/lib/Basic/Targets/X86.cpp @@ -399,9 +399,6 @@ return false; } - SimdDefaultAlign = - hasFeature("avx512f") ? 512 : hasFeature("avx") ? 256 : 128; - // FIXME: We should allow long double type on 32-bits to match with GCC. // This requires backend to be able to lower f80 without x87 first. if (!HasX87 && LongDoubleFormat == &llvm::APFloat::x87DoubleExtended()) Index: lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp =================================================================== --- lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp +++ lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp @@ -499,8 +499,6 @@ auto target_info = TargetInfo::CreateTargetInfo( m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts); if (log) { - LLDB_LOGF(log, "Using SIMD alignment: %d", - target_info->getSimdDefaultAlign()); LLDB_LOGF(log, "Target datalayout string: '%s'", target_info->getDataLayoutString()); LLDB_LOGF(log, "Target ABI: '%s'", target_info->getABI().str().c_str()); Index: llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h =================================================================== --- llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h +++ llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h @@ -491,6 +491,18 @@ ArrayRef Loops, InsertPointTy ComputeIP); + /// Get the alignment value for given target and type of aligned value + /// + /// \param Triple String which describes target triple + /// \param CPU String which describes target CPU + /// \param Features String which describes extra CPU features + /// \param AlignedValueTy Type of the value which needs to be aligned. + /// If it is nullptr then the alignment is calculated + /// on the basis of target CPU properties. + static unsigned getVectorTypeAlignment(const std::string &Triple, + StringRef CPU, StringRef Features, + Type *AlignedValueTy = nullptr); + private: /// Modifies the canonical loop to be a statically-scheduled workshare loop. /// Index: llvm/include/llvm/Target/TargetMachine.h =================================================================== --- llvm/include/llvm/Target/TargetMachine.h +++ llvm/include/llvm/Target/TargetMachine.h @@ -106,6 +106,9 @@ std::unique_ptr MII; std::unique_ptr STI; + /// OpenMP Target Specific information + unsigned OpenMPSimdMaxAlignment = 0; + unsigned RequireStructuredCFG : 1; unsigned O0WantsFastISel : 1; @@ -195,6 +198,9 @@ return DL.getPointerSize(DL.getAllocaAddrSpace()); } + /// Return maximal allowed SIMD alignment + unsigned getMaximalSimdAlignment() const { return OpenMPSimdMaxAlignment; } + /// Reset the target options based on the function's attributes. // FIXME: Remove TargetOptions that affect per-function code generation // from TargetMachine. Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp =================================================================== --- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -3061,6 +3061,64 @@ Builder.CreateBr(NewBlocks.front()); } +unsigned OpenMPIRBuilder::getVectorTypeAlignment(const std::string &Triple, + StringRef CPU, + StringRef Features, + llvm::Type *AlignedValueTy) { + + std::string Error; + unsigned MaxSimdAlignment; + static std::string PreviousTripleString; + static std::string PreviousCPUString; + static std::string PreviousFeaturesString; + static std::unique_ptr TgtInfo; + const Target *TheTarget; + bool UpdateTargetMachine = false; + // Check if we need to update target machine + if (PreviousTripleString != Triple) { + UpdateTargetMachine = true; + PreviousTripleString = Triple; + } + if (!CPU.equals(PreviousCPUString)) { + UpdateTargetMachine = true; + PreviousCPUString = CPU.str(); + } + if (!Features.equals(PreviousFeaturesString)) { + UpdateTargetMachine = true; + PreviousFeaturesString = Features.str(); + } + // Update target machine only if there is need to do it + if (UpdateTargetMachine) { + TheTarget = TargetRegistry::lookupTarget(Triple, Error); + if (!TheTarget) { + return 0; + } + TargetOptions Options; + TgtInfo = + std::move(std::unique_ptr(TheTarget->createTargetMachine( + Triple, CPU, Features, Options, None, None, CodeGenOpt::Default))); + } + if (!TgtInfo) { + return 0; + } + + MaxSimdAlignment = TgtInfo->getMaximalSimdAlignment(); + // If aligned value type is not specified, then return maximal target + // alignment for given target. + if (!AlignedValueTy) { + return MaxSimdAlignment; + } + + const DataLayout &DL = TgtInfo->createDataLayout(); + Align PrefAlign = DL.getPrefTypeAlign(AlignedValueTy); + + // If preffered alignment is bigger than maximal, return the maximal value + if (PrefAlign.value() > MaxSimdAlignment) { + return MaxSimdAlignment; + } + return PrefAlign.value(); +} + void OpenMPIRBuilder::applySimd(CanonicalLoopInfo *CanonicalLoop, MapVector AlignedVars, Value *IfCond, OrderKind Order, Index: llvm/lib/Target/PowerPC/PPCTargetMachine.cpp =================================================================== --- llvm/lib/Target/PowerPC/PPCTargetMachine.cpp +++ llvm/lib/Target/PowerPC/PPCTargetMachine.cpp @@ -334,6 +334,7 @@ TLOF(createTLOF(getTargetTriple())), TargetABI(computeTargetABI(TT, Options)), Endianness(isLittleEndianTriple(TT) ? Endian::LITTLE : Endian::BIG) { + OpenMPSimdMaxAlignment = 128; initAsmInfo(); } Index: llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp =================================================================== --- llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp +++ llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp @@ -135,6 +135,7 @@ this->Options.FunctionSections = true; this->Options.DataSections = true; this->Options.UniqueSectionNames = true; + OpenMPSimdMaxAlignment = 128; initAsmInfo(); Index: llvm/lib/Target/X86/X86TargetMachine.cpp =================================================================== --- llvm/lib/Target/X86/X86TargetMachine.cpp +++ llvm/lib/Target/X86/X86TargetMachine.cpp @@ -243,6 +243,13 @@ setSupportsDebugEntryValues(true); initAsmInfo(); + + if (FS.contains("+avx512f")) + OpenMPSimdMaxAlignment = 512; + else if (FS.contains("+avx")) + OpenMPSimdMaxAlignment = 256; + else + OpenMPSimdMaxAlignment = 128; } X86TargetMachine::~X86TargetMachine() = default;