Skip to content

Commit 80abf82

Browse files
SC llvm teamSC llvm team
SC llvm team
authored and
SC llvm team
committed
Merged main:b4afade17564 into amd-gfx:259556a2c143
Local branch amd-gfx 259556a Merged main:7ac532efc8ae into amd-gfx:d8f80774ebd7 Remote branch main b4afade [InstCombine] Avoid use of ConstantExpr::getZExtOrBitcast() (NFC)
2 parents 259556a + b4afade commit 80abf82

39 files changed

+535
-140
lines changed

clang/docs/OpenMPSupport.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
.. contents::
1414
:local:
1515

16+
==============
1617
OpenMP Support
1718
==============
1819

clang/include/clang/Basic/Attr.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,8 @@ def CUDAInvalidTarget : InheritableAttr {
12751275

12761276
def CUDALaunchBounds : InheritableAttr {
12771277
let Spellings = [GNU<"launch_bounds">, Declspec<"__launch_bounds__">];
1278-
let Args = [ExprArgument<"MaxThreads">, ExprArgument<"MinBlocks", 1>];
1278+
let Args = [ExprArgument<"MaxThreads">, ExprArgument<"MinBlocks", 1>,
1279+
ExprArgument<"MaxBlocks", 1>];
12791280
let LangOpts = [CUDA];
12801281
let Subjects = SubjectList<[ObjCMethod, FunctionLike]>;
12811282
// An AST node is created for this attribute, but is not used by other parts

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11850,6 +11850,10 @@ def err_sycl_special_type_num_init_method : Error<
1185011850
"types with 'sycl_special_class' attribute must have one and only one '__init' "
1185111851
"method defined">;
1185211852

11853+
def warn_cuda_maxclusterrank_sm_90 : Warning<
11854+
"maxclusterrank requires sm_90 or higher, CUDA arch provided: %0, ignoring "
11855+
"%1 attribute">, InGroup<IgnoredAttributes>;
11856+
1185311857
def err_bit_int_bad_size : Error<"%select{signed|unsigned}0 _BitInt must "
1185411858
"have a bit size of at least %select{2|1}0">;
1185511859
def err_bit_int_max_size : Error<"%select{signed|unsigned}0 _BitInt of bit "

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11053,12 +11053,13 @@ class Sema final {
1105311053
/// Create an CUDALaunchBoundsAttr attribute.
1105411054
CUDALaunchBoundsAttr *CreateLaunchBoundsAttr(const AttributeCommonInfo &CI,
1105511055
Expr *MaxThreads,
11056-
Expr *MinBlocks);
11056+
Expr *MinBlocks,
11057+
Expr *MaxBlocks);
1105711058

1105811059
/// AddLaunchBoundsAttr - Adds a launch_bounds attribute to a particular
1105911060
/// declaration.
1106011061
void AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI,
11061-
Expr *MaxThreads, Expr *MinBlocks);
11062+
Expr *MaxThreads, Expr *MinBlocks, Expr *MaxBlocks);
1106211063

1106311064
/// AddModeAttr - Adds a mode attribute to a particular declaration.
1106411065
void AddModeAttr(Decl *D, const AttributeCommonInfo &CI, IdentifierInfo *Name,

clang/lib/AST/Interp/Boolean.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class Boolean final {
8484
Boolean truncate(unsigned TruncBits) const { return *this; }
8585

8686
void print(llvm::raw_ostream &OS) const { OS << (V ? "true" : "false"); }
87+
std::string toDiagnosticString(const ASTContext &Ctx) const {
88+
std::string NameStr;
89+
llvm::raw_string_ostream OS(NameStr);
90+
print(OS);
91+
return NameStr;
92+
}
8793

8894
static Boolean min(unsigned NumBits) { return Boolean(false); }
8995
static Boolean max(unsigned NumBits) { return Boolean(true); }

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,29 @@ bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
253253
return this->delegate(RHS);
254254
}
255255

256+
// Special case for C++'s three-way/spaceship operator <=>, which
257+
// returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
258+
// have a PrimType).
259+
if (!T) {
260+
if (DiscardResult)
261+
return true;
262+
const ComparisonCategoryInfo *CmpInfo =
263+
Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
264+
assert(CmpInfo);
265+
266+
// We need a temporary variable holding our return value.
267+
if (!Initializing) {
268+
std::optional<unsigned> ResultIndex = this->allocateLocal(BO, false);
269+
if (!this->emitGetPtrLocal(*ResultIndex, BO))
270+
return false;
271+
}
272+
273+
if (!visit(LHS) || !visit(RHS))
274+
return false;
275+
276+
return this->emitCMP3(*LT, CmpInfo, BO);
277+
}
278+
256279
if (!LT || !RT || !T)
257280
return this->bail(BO);
258281

clang/lib/AST/Interp/Floating.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ class Floating final {
7676
F.toString(Buffer);
7777
OS << Buffer;
7878
}
79+
std::string toDiagnosticString(const ASTContext &Ctx) const {
80+
std::string NameStr;
81+
llvm::raw_string_ostream OS(NameStr);
82+
print(OS);
83+
return NameStr;
84+
}
7985

8086
unsigned bitWidth() const { return F.semanticsSizeInBits(F.getSemantics()); }
8187

clang/lib/AST/Interp/Integral.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ template <unsigned Bits, bool Signed> class Integral final {
128128
return Compare(V, RHS.V);
129129
}
130130

131+
std::string toDiagnosticString(const ASTContext &Ctx) const {
132+
std::string NameStr;
133+
llvm::raw_string_ostream OS(NameStr);
134+
OS << V;
135+
return NameStr;
136+
}
137+
131138
unsigned countLeadingZeros() const {
132139
if constexpr (!Signed)
133140
return llvm::countl_zero<ReprT>(V);

clang/lib/AST/Interp/Interp.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ bool CheckCtorCall(InterpState &S, CodePtr OpPC, const Pointer &This);
112112
bool CheckPotentialReinterpretCast(InterpState &S, CodePtr OpPC,
113113
const Pointer &Ptr);
114114

115+
/// Sets the given integral value to the pointer, which is of
116+
/// a std::{weak,partial,strong}_ordering type.
117+
bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC,
118+
const Pointer &Ptr, const APSInt &IntValue);
119+
115120
/// Checks if the shift operation is legal.
116121
template <typename LT, typename RT>
117122
bool CheckShift(InterpState &S, CodePtr OpPC, const LT &LHS, const RT &RHS,
@@ -781,6 +786,30 @@ bool EQ(InterpState &S, CodePtr OpPC) {
781786
});
782787
}
783788

789+
template <PrimType Name, class T = typename PrimConv<Name>::T>
790+
bool CMP3(InterpState &S, CodePtr OpPC, const ComparisonCategoryInfo *CmpInfo) {
791+
const T &RHS = S.Stk.pop<T>();
792+
const T &LHS = S.Stk.pop<T>();
793+
const Pointer &P = S.Stk.peek<Pointer>();
794+
795+
ComparisonCategoryResult CmpResult = LHS.compare(RHS);
796+
if (CmpResult == ComparisonCategoryResult::Unordered) {
797+
// This should only happen with pointers.
798+
const SourceInfo &Loc = S.Current->getSource(OpPC);
799+
S.FFDiag(Loc, diag::note_constexpr_pointer_comparison_unspecified)
800+
<< LHS.toDiagnosticString(S.getCtx())
801+
<< RHS.toDiagnosticString(S.getCtx());
802+
return false;
803+
}
804+
805+
assert(CmpInfo);
806+
const auto *CmpValueInfo = CmpInfo->getValueInfo(CmpResult);
807+
assert(CmpValueInfo);
808+
assert(CmpValueInfo->hasValidIntValue());
809+
APSInt IntValue = CmpValueInfo->getIntValue();
810+
return SetThreeWayComparisonField(S, OpPC, P, IntValue);
811+
}
812+
784813
template <PrimType Name, class T = typename PrimConv<Name>::T>
785814
bool NE(InterpState &S, CodePtr OpPC) {
786815
return CmpHelperEQ<T>(S, OpPC, [](ComparisonCategoryResult R) {

clang/lib/AST/Interp/InterpBuiltin.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,5 +594,22 @@ bool InterpretOffsetOf(InterpState &S, CodePtr OpPC, const OffsetOfExpr *E,
594594
return true;
595595
}
596596

597+
bool SetThreeWayComparisonField(InterpState &S, CodePtr OpPC,
598+
const Pointer &Ptr, const APSInt &IntValue) {
599+
600+
const Record *R = Ptr.getRecord();
601+
assert(R);
602+
assert(R->getNumFields() == 1);
603+
604+
unsigned FieldOffset = R->getField(0u)->Offset;
605+
const Pointer &FieldPtr = Ptr.atField(FieldOffset);
606+
PrimType FieldT = *S.getContext().classify(FieldPtr.getType());
607+
608+
INT_TYPE_SWITCH(FieldT,
609+
FieldPtr.deref<T>() = T::from(IntValue.getSExtValue()));
610+
FieldPtr.initialize();
611+
return true;
612+
}
613+
597614
} // namespace interp
598615
} // namespace clang

clang/lib/AST/Interp/Opcodes.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def ArgCastKind : ArgType { let Name = "CastKind"; }
5555
def ArgCallExpr : ArgType { let Name = "const CallExpr *"; }
5656
def ArgOffsetOfExpr : ArgType { let Name = "const OffsetOfExpr *"; }
5757
def ArgDeclRef : ArgType { let Name = "const DeclRefExpr *"; }
58+
def ArgCCI : ArgType { let Name = "const ComparisonCategoryInfo *"; }
5859

5960
//===----------------------------------------------------------------------===//
6061
// Classes of types instructions operate on.
@@ -607,6 +608,10 @@ class ComparisonOpcode : Opcode {
607608
let HasGroup = 1;
608609
}
609610

611+
def CMP3 : ComparisonOpcode {
612+
let Args = [ArgCCI];
613+
}
614+
610615
def LT : ComparisonOpcode;
611616
def LE : ComparisonOpcode;
612617
def GT : ComparisonOpcode;

clang/lib/AST/Interp/Pointer.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,19 @@ class Pointer {
362362
/// Deactivates an entire strurcutre.
363363
void deactivate() const;
364364

365+
/// Compare two pointers.
366+
ComparisonCategoryResult compare(const Pointer &Other) const {
367+
if (!hasSameBase(*this, Other))
368+
return ComparisonCategoryResult::Unordered;
369+
370+
if (Offset < Other.Offset)
371+
return ComparisonCategoryResult::Less;
372+
else if (Offset > Other.Offset)
373+
return ComparisonCategoryResult::Greater;
374+
375+
return ComparisonCategoryResult::Equal;
376+
}
377+
365378
/// Checks if two pointers are comparable.
366379
static bool hasSameBase(const Pointer &A, const Pointer &B);
367380
/// Checks if two pointers can be subtracted.

clang/lib/Basic/Targets/NVPTX.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class LLVM_LIBRARY_VISIBILITY NVPTXTargetInfo : public TargetInfo {
181181

182182
bool hasBitIntType() const override { return true; }
183183
bool hasBFloat16Type() const override { return true; }
184+
185+
CudaArch getGPU() const { return GPU; }
184186
};
185187
} // namespace targets
186188
} // namespace clang

clang/lib/CodeGen/Targets/NVPTX.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ void CodeGenModule::handleCUDALaunchBoundsAttr(
296296
NVPTXTargetCodeGenInfo::addNVVMMetadata(F, "maxntidx",
297297
MaxThreads.getExtValue());
298298

299-
// min blocks is an optional argument for CUDALaunchBoundsAttr. If it was
300-
// not specified in __launch_bounds__ or if the user specified a 0 value,
299+
// min and max blocks is an optional argument for CUDALaunchBoundsAttr. If it
300+
// was not specified in __launch_bounds__ or if the user specified a 0 value,
301301
// we don't have to add a PTX directive.
302302
if (Attr->getMinBlocks()) {
303303
llvm::APSInt MinBlocks(32);
@@ -307,6 +307,14 @@ void CodeGenModule::handleCUDALaunchBoundsAttr(
307307
NVPTXTargetCodeGenInfo::addNVVMMetadata(F, "minctasm",
308308
MinBlocks.getExtValue());
309309
}
310+
if (Attr->getMaxBlocks()) {
311+
llvm::APSInt MaxBlocks(32);
312+
MaxBlocks = Attr->getMaxBlocks()->EvaluateKnownConstInt(getContext());
313+
if (MaxBlocks > 0)
314+
// Create !{<func-ref>, metadata !"maxclusterrank", i32 <val>} node
315+
NVPTXTargetCodeGenInfo::addNVVMMetadata(F, "maxclusterrank",
316+
MaxBlocks.getExtValue());
317+
}
310318
}
311319

312320
std::unique_ptr<TargetCodeGenInfo>

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3739,7 +3739,8 @@ OMPClause *Parser::ParseOpenMPOMPXAttributesClause(bool ParseOnly) {
37393739
continue;
37403740
if (auto *A = Actions.CreateLaunchBoundsAttr(
37413741
PA, PA.getArgAsExpr(0),
3742-
PA.getNumArgs() > 1 ? PA.getArgAsExpr(1) : nullptr))
3742+
PA.getNumArgs() > 1 ? PA.getArgAsExpr(1) : nullptr,
3743+
PA.getNumArgs() > 2 ? PA.getArgAsExpr(2) : nullptr))
37433744
Attrs.push_back(A);
37443745
continue;
37453746
default:

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5608,6 +5608,14 @@ bool Sema::CheckRegparmAttr(const ParsedAttr &AL, unsigned &numParams) {
56085608
return false;
56095609
}
56105610

5611+
// Helper to get CudaArch.
5612+
static CudaArch getCudaArch(const TargetInfo &TI) {
5613+
if (!TI.getTriple().isNVPTX())
5614+
llvm_unreachable("getCudaArch is only valid for NVPTX triple");
5615+
auto &TO = TI.getTargetOpts();
5616+
return StringToCudaArch(TO.CPU);
5617+
}
5618+
56115619
// Checks whether an argument of launch_bounds attribute is
56125620
// acceptable, performs implicit conversion to Rvalue, and returns
56135621
// non-nullptr Expr result on success. Otherwise, it returns nullptr
@@ -5651,34 +5659,51 @@ static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E,
56515659

56525660
CUDALaunchBoundsAttr *
56535661
Sema::CreateLaunchBoundsAttr(const AttributeCommonInfo &CI, Expr *MaxThreads,
5654-
Expr *MinBlocks) {
5655-
CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks);
5662+
Expr *MinBlocks, Expr *MaxBlocks) {
5663+
CUDALaunchBoundsAttr TmpAttr(Context, CI, MaxThreads, MinBlocks, MaxBlocks);
56565664
MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0);
5657-
if (MaxThreads == nullptr)
5665+
if (!MaxThreads)
56585666
return nullptr;
56595667

56605668
if (MinBlocks) {
56615669
MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1);
5662-
if (MinBlocks == nullptr)
5670+
if (!MinBlocks)
56635671
return nullptr;
56645672
}
56655673

5674+
if (MaxBlocks) {
5675+
// '.maxclusterrank' ptx directive requires .target sm_90 or higher.
5676+
auto SM = getCudaArch(Context.getTargetInfo());
5677+
if (SM == CudaArch::UNKNOWN || SM < CudaArch::SM_90) {
5678+
Diag(MaxBlocks->getBeginLoc(), diag::warn_cuda_maxclusterrank_sm_90)
5679+
<< CudaArchToString(SM) << CI << MaxBlocks->getSourceRange();
5680+
// Ignore it by setting MaxBlocks to null;
5681+
MaxBlocks = nullptr;
5682+
} else {
5683+
MaxBlocks = makeLaunchBoundsArgExpr(*this, MaxBlocks, TmpAttr, 2);
5684+
if (!MaxBlocks)
5685+
return nullptr;
5686+
}
5687+
}
5688+
56665689
return ::new (Context)
5667-
CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks);
5690+
CUDALaunchBoundsAttr(Context, CI, MaxThreads, MinBlocks, MaxBlocks);
56685691
}
56695692

56705693
void Sema::AddLaunchBoundsAttr(Decl *D, const AttributeCommonInfo &CI,
5671-
Expr *MaxThreads, Expr *MinBlocks) {
5672-
if (auto *Attr = CreateLaunchBoundsAttr(CI, MaxThreads, MinBlocks))
5694+
Expr *MaxThreads, Expr *MinBlocks,
5695+
Expr *MaxBlocks) {
5696+
if (auto *Attr = CreateLaunchBoundsAttr(CI, MaxThreads, MinBlocks, MaxBlocks))
56735697
D->addAttr(Attr);
56745698
}
56755699

56765700
static void handleLaunchBoundsAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5677-
if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 2))
5701+
if (!AL.checkAtLeastNumArgs(S, 1) || !AL.checkAtMostNumArgs(S, 3))
56785702
return;
56795703

56805704
S.AddLaunchBoundsAttr(D, AL, AL.getArgAsExpr(0),
5681-
AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr);
5705+
AL.getNumArgs() > 1 ? AL.getArgAsExpr(1) : nullptr,
5706+
AL.getNumArgs() > 2 ? AL.getArgAsExpr(2) : nullptr);
56825707
}
56835708

56845709
static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D,

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,15 @@ static void instantiateDependentCUDALaunchBoundsAttr(
302302
MinBlocks = Result.getAs<Expr>();
303303
}
304304

305-
S.AddLaunchBoundsAttr(New, Attr, MaxThreads, MinBlocks);
305+
Expr *MaxBlocks = nullptr;
306+
if (Attr.getMaxBlocks()) {
307+
Result = S.SubstExpr(Attr.getMaxBlocks(), TemplateArgs);
308+
if (Result.isInvalid())
309+
return;
310+
MaxBlocks = Result.getAs<Expr>();
311+
}
312+
313+
S.AddLaunchBoundsAttr(New, Attr, MaxThreads, MinBlocks, MaxBlocks);
306314
}
307315

308316
static void

0 commit comments

Comments
 (0)