Skip to content

[CodeGen] Emit a more accurate alignment for non-temporal loads/stores #75675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

ahatanak
Copy link
Collaborator

Call EmitPointerWithAlignment to compute the alignment based on the underlying lvalue's alignment when it's available.

Call EmitPointerWithAlignment to compute the alignment based on the
underlying lvalue's alignment when it's available.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Dec 16, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2023

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: Akira Hatanaka (ahatanak)

Changes

Call EmitPointerWithAlignment to compute the alignment based on the underlying lvalue's alignment when it's available.


Full diff: https://github.com/llvm/llvm-project/pull/75675.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CGBuiltin.cpp (+4-4)
  • (modified) clang/test/CodeGen/Nontemporal.cpp (+14)
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 3327866d2b9623..c96f86a823a461 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -232,19 +232,19 @@ static Value *MakeBinaryAtomicValue(
 
 static Value *EmitNontemporalStore(CodeGenFunction &CGF, const CallExpr *E) {
   Value *Val = CGF.EmitScalarExpr(E->getArg(0));
-  Value *Address = CGF.EmitScalarExpr(E->getArg(1));
+  Address Addr = CGF.EmitPointerWithAlignment(E->getArg(1));
 
   Val = CGF.EmitToMemory(Val, E->getArg(0)->getType());
-  LValue LV = CGF.MakeNaturalAlignAddrLValue(Address, E->getArg(0)->getType());
+  LValue LV = CGF.MakeAddrLValue(Addr, E->getArg(0)->getType());
   LV.setNontemporal(true);
   CGF.EmitStoreOfScalar(Val, LV, false);
   return nullptr;
 }
 
 static Value *EmitNontemporalLoad(CodeGenFunction &CGF, const CallExpr *E) {
-  Value *Address = CGF.EmitScalarExpr(E->getArg(0));
+  Address Addr = CGF.EmitPointerWithAlignment(E->getArg(0));
 
-  LValue LV = CGF.MakeNaturalAlignAddrLValue(Address, E->getType());
+  LValue LV = CGF.MakeAddrLValue(Addr, E->getType());
   LV.setNontemporal(true);
   return CGF.EmitLoadOfScalar(LV, E->getExprLoc());
 }
diff --git a/clang/test/CodeGen/Nontemporal.cpp b/clang/test/CodeGen/Nontemporal.cpp
index e14ca18717928d..5052cb225d4111 100644
--- a/clang/test/CodeGen/Nontemporal.cpp
+++ b/clang/test/CodeGen/Nontemporal.cpp
@@ -46,3 +46,17 @@ void test_all_sizes(void)                 // CHECK-LABEL: test_all_sizes
   vf2 = __builtin_nontemporal_load(&vf1); // CHECK: load <4 x float>{{.*}}align 16, !nontemporal
   vc2 = __builtin_nontemporal_load(&vc1); // CHECK: load <8 x i8>{{.*}}align 8, !nontemporal
 }
+
+struct S { char c[16]; };
+S x;
+
+typedef int v4si __attribute__ ((vector_size(16)));
+
+// CHECK-LABEL: define void @_Z14test_alignmentv()
+// CHECK: load <4 x i32>, ptr @x, align 1, !nontemporal
+// CHECK: store <4 x i32> %1, ptr @x, align 1, !nontemporal
+
+void test_alignment() {
+ auto t =  __builtin_nontemporal_load((v4si*)x.c);
+ __builtin_nontemporal_store(t, (v4si*)x.c);
+}

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ahatanak ahatanak merged commit 31429e7 into llvm:main Dec 18, 2023
@ahatanak ahatanak deleted the alignment-non-temporal branch December 18, 2023 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants