[PATCH] D71499: Add builtins for aligning and checking alignment of pointers and integers

Roman Lebedev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 2 10:26:56 PST 2020


lebedev.ri added a comment.

Hmm, i keep coming up with things here, sorry :/

I think we should take one more step - explicitly call out that the result of `__builtin_align_*` is, well, aligned :)
For that, we need to add `__attribute__((alloc_align(2)))` attribute.
Despite the name, it does not imply anything about provenance/aliasing/etc, only about alignment:
https://godbolt.org/z/9bAjxK



================
Comment at: clang/docs/LanguageExtensions.rst:2854-2855
+qualifiers such as ``const``) with an adjusted address.
+When aligning pointers up or down, the resulting value must be within the same
+underlyig allocation (or one past the end), i.e., arbitrary integer values
+stored in pointer-type variables must not be passed to these builtins.
----------------
Might be worth explicitly calling out C17 6.5.6p8, C++ [expr.add]?


================
Comment at: clang/lib/CodeGen/CGBuiltin.cpp:14329-14330
+    // can use an inbounds GEP to enable better optimization.
+    Result = Builder.CreateInBoundsGEP(EmitCastToVoidPtr(Args.Src), Difference,
+                                       "aligned_result");
+    Result = Builder.CreatePointerCast(Result, Args.SrcType);
----------------
I would suspect this should follow suit of the rest of clang codegen, i.e. do
```
Value* Base = EmitCastToVoidPtr(Args.Src);
if (CGF.getLangOpts().isSignedOverflowDefined())
  Result = Builder.CreateGEP(Base, Difference, "aligned_result");
else
  Result = CGF.EmitCheckedInBoundsGEP(Base, Difference,
                 /*SignedIndices=*/true, /*isSubtraction*/=false,
                 E->getExprLoc(), "aligned_result");
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71499/new/

https://reviews.llvm.org/D71499





More information about the cfe-commits mailing list