Skip to content

Commit d096f8d

Browse files
committed
[IR] Attribute/AttrBuilder: use Value::MaximumAlignment magic constant
Summary: I initially encountered those assertions when trying to create this IR `alignment` attribute from clang's `__attribute__((assume_aligned(imm)))`, because until D72994 there is no sanity checking for the value of `imm`. But even then, we have `llvm::Value::MaximumAlignment` constant (which is `536870912`), which is enforced for clang attributes, and then there are some other magical constant (`0x40000000` i.e. `1073741824` i.e. `2 * 536870912`) in `Attribute::getWithAlignment()`/`AttrBuilder::addAlignmentAttr()`. I strongly suspect that `0x40000000` is incorrect, and that also should be `llvm::Value::MaximumAlignment`. Reviewers: erichkeane, hfinkel, jdoerfert, gchatelet, courbet Reviewed By: erichkeane Subscribers: hiraditya, cfe-commits, llvm-commits Tags: #llvm, #clang Differential Revision: https://reviews.llvm.org/D72998
1 parent c2a9061 commit d096f8d

File tree

4 files changed

+20
-21
lines changed

4 files changed

+20
-21
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,15 @@ class Sema final {
372372
QualType ResultTy,
373373
ArrayRef<QualType> Args);
374374

375+
/// The maximum alignment, same as in llvm::Value. We duplicate them here
376+
/// because that allows us not to duplicate the constants in clang code,
377+
/// which we must to since we can't directly use the llvm constants.
378+
///
379+
/// This is the greatest alignment value supported by load, store, and alloca
380+
/// instructions, and global values.
381+
static const unsigned MaxAlignmentExponent = 29;
382+
static const unsigned MaximumAlignment = 1u << MaxAlignmentExponent;
383+
375384
public:
376385
typedef OpaquePtr<DeclGroupRef> DeclGroupPtrTy;
377386
typedef OpaquePtr<TemplateName> TemplateTy;

clang/lib/Sema/SemaChecking.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3665,11 +3665,9 @@ void Sema::checkCall(NamedDecl *FDecl, const FunctionProtoType *Proto,
36653665
return;
36663666
}
36673667

3668-
// Alignment calculations can wrap around if it's greater than 2**29.
3669-
unsigned MaximumAlignment = 536870912;
3670-
if (I > MaximumAlignment)
3668+
if (I > Sema::MaximumAlignment)
36713669
Diag(Arg->getExprLoc(), diag::warn_assume_aligned_too_great)
3672-
<< Arg->getSourceRange() << MaximumAlignment;
3670+
<< Arg->getSourceRange() << Sema::MaximumAlignment;
36733671
}
36743672
}
36753673
}
@@ -5394,11 +5392,9 @@ bool Sema::SemaBuiltinAssumeAligned(CallExpr *TheCall) {
53945392
return Diag(TheCall->getBeginLoc(), diag::err_alignment_not_power_of_two)
53955393
<< Arg->getSourceRange();
53965394

5397-
// Alignment calculations can wrap around if it's greater than 2**29.
5398-
unsigned MaximumAlignment = 536870912;
5399-
if (Result > MaximumAlignment)
5395+
if (Result > Sema::MaximumAlignment)
54005396
Diag(TheCall->getBeginLoc(), diag::warn_assume_aligned_too_great)
5401-
<< Arg->getSourceRange() << MaximumAlignment;
5397+
<< Arg->getSourceRange() << Sema::MaximumAlignment;
54025398
}
54035399

54045400
if (NumArgs > 2) {

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,11 +1626,9 @@ void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
16261626
return;
16271627
}
16281628

1629-
// Alignment calculations can wrap around if it's greater than 2**29.
1630-
unsigned MaximumAlignment = 536870912;
1631-
if (I > MaximumAlignment)
1629+
if (I > Sema::MaximumAlignment)
16321630
Diag(CI.getLoc(), diag::warn_assume_aligned_too_great)
1633-
<< CI.getRange() << MaximumAlignment;
1631+
<< CI.getRange() << Sema::MaximumAlignment;
16341632
}
16351633

16361634
if (OE) {
@@ -3815,13 +3813,9 @@ void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
38153813
}
38163814
}
38173815

3818-
// Alignment calculations can wrap around if it's greater than 2**28.
3819-
unsigned MaxValidAlignment =
3820-
Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192
3821-
: 268435456;
3822-
if (AlignVal > MaxValidAlignment) {
3823-
Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment
3824-
<< E->getSourceRange();
3816+
if (AlignVal > Sema::MaximumAlignment) {
3817+
Diag(AttrLoc, diag::err_attribute_aligned_too_great)
3818+
<< Sema::MaximumAlignment << E->getSourceRange();
38253819
return;
38263820
}
38273821

llvm/lib/IR/Attributes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Attribute Attribute::get(LLVMContext &Context, Attribute::AttrKind Kind,
143143
}
144144

145145
Attribute Attribute::getWithAlignment(LLVMContext &Context, Align A) {
146-
assert(A <= 0x40000000 && "Alignment too large.");
146+
assert(A <= llvm::Value::MaximumAlignment && "Alignment too large.");
147147
return get(Context, Alignment, A.value());
148148
}
149149

@@ -1525,7 +1525,7 @@ AttrBuilder &AttrBuilder::addAlignmentAttr(MaybeAlign Align) {
15251525
if (!Align)
15261526
return *this;
15271527

1528-
assert(*Align <= 0x40000000 && "Alignment too large.");
1528+
assert(*Align <= llvm::Value::MaximumAlignment && "Alignment too large.");
15291529

15301530
Attrs[Attribute::Alignment] = true;
15311531
Alignment = Align;

0 commit comments

Comments
 (0)