[PATCH] D129542: [CodeGen] Add codegen of IR function attribute fine_grained_bitfields

John McIver via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 12 10:34:09 PDT 2022


jmciver created this revision.
Herald added a project: All.
jmciver published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This change helps to prevents mixing fine grained and non fine grained bit-field
addressing schemes, which can result in incorrect poison state at
initialization. The IR function attribute fine_grained_bitfields is only added
when fine grained bitfield accesses are enable. The attribute is used by opt to
prevent inlining.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129542

Files:
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/test/CodeGen/fine-grained-bitfield-accesses.c


Index: clang/test/CodeGen/fine-grained-bitfield-accesses.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/fine-grained-bitfield-accesses.c
@@ -0,0 +1,21 @@
+// RUN: %clang -ffine-grained-bitfield-accesses -S -emit-llvm -o - %s | FileCheck %s
+// CHECK: define{{.*}} @g(){{.*}} #[[GATTR:[0-9]+]] {
+// CHECK: declare{{.*}} void @f(ptr noundef){{.*}} #[[FATTR:[0-9]+]]
+// CHECK: attributes #[[GATTR]] = {{.*}} fine_grained_bitfields {{.*}}
+// CHECK: attributes #[[FATTR]] = {{.*}} fine_grained_bitfields {{.*}}
+//
+// Verify that the clang fine-grained-bitfield-accesses option adds the IR
+// function attribute fine_grained_bitfields.
+struct X {
+  int a : 8;
+  int b : 24;
+};
+
+void f(struct X*);
+
+int g() {
+  struct X x;
+  x.a = 10;
+  f(&x);
+  return x.a;
+}
Index: clang/lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.cpp
+++ clang/lib/CodeGen/CodeGenFunction.cpp
@@ -984,6 +984,11 @@
     Fn->addFnAttr(llvm::Attribute::StrictFP);
   }
 
+  // The fine grained bit-fields attribute is used to determine IPO inlining
+  // compatibility.
+  if (getTypes().getCodeGenOpts().FineGrainedBitfieldAccesses)
+    Fn->addFnAttr(llvm::Attribute::FineGrainedBitfields);
+
   // If a custom alignment is used, force realigning to this alignment on
   // any main function which certainly will need it.
   if (FD && ((FD->isMain() || FD->isMSVCRTEntryPoint()) &&
Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -1890,6 +1890,9 @@
     if (CodeGenOpts.SpeculativeLoadHardening)
       FuncAttrs.addAttribute(llvm::Attribute::SpeculativeLoadHardening);
 
+    if (getTypes().getCodeGenOpts().FineGrainedBitfieldAccesses)
+      FuncAttrs.addAttribute(llvm::Attribute::FineGrainedBitfields);
+
     // Add zero-call-used-regs attribute.
     switch (CodeGenOpts.getZeroCallUsedRegs()) {
     case llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129542.443846.patch
Type: text/x-patch
Size: 2125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220712/6adc381f/attachment.bin>


More information about the cfe-commits mailing list