Skip to content

[Flang][Fir] Set default alignment of array globals to 64 bytes#194969

Open
Jason-Van-Beusekom wants to merge 1 commit intollvm:mainfrom
Jason-Van-Beusekom:static-global-align
Open

[Flang][Fir] Set default alignment of array globals to 64 bytes#194969
Jason-Van-Beusekom wants to merge 1 commit intollvm:mainfrom
Jason-Van-Beusekom:static-global-align

Conversation

@Jason-Van-Beusekom
Copy link
Copy Markdown
Contributor

This commit implements the proposal from the RFC:
https://discourse.llvm.org/t/rfc-alignment-of-global-arrays/90397/13

This PR sets the alignment of all global arrays to 64 bytes (except for BIND(C) and common blocks). This Mirrors the execution of other fortran compilers (CCE, gfortran, nvfortran and ifx).

@llvmorg-github-actions
Copy link
Copy Markdown

llvmorg-github-actions Bot commented Apr 29, 2026

@llvm/pr-subscribers-flang-fir-hlfir
@llvm/pr-subscribers-openacc

@llvm/pr-subscribers-flang-openmp

Author: Jason Van Beusekom (Jason-Van-Beusekom)

Changes

This commit implements the proposal from the RFC:
https://discourse.llvm.org/t/rfc-alignment-of-global-arrays/90397/13

This PR sets the alignment of all global arrays to 64 bytes (except for BIND(C) and common blocks). This Mirrors the execution of other fortran compilers (CCE, gfortran, nvfortran and ifx).


Patch is 39.70 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/194969.diff

32 Files Affected:

  • (modified) flang/lib/Lower/ConvertVariable.cpp (+9)
  • (modified) flang/lib/Optimizer/Builder/FIRBuilder.cpp (+12)
  • (modified) flang/test/Lower/CUDA/cuda-data-attribute.cuf (+3-3)
  • (modified) flang/test/Lower/CUDA/cuda-program-global.cuf (+1-1)
  • (modified) flang/test/Lower/HLFIR/tdesc-character-comp-init.f90 (+1-1)
  • (modified) flang/test/Lower/Intrinsics/ieee_class.f90 (+2-2)
  • (modified) flang/test/Lower/OpenACC/acc-declare-globals.f90 (+3-3)
  • (modified) flang/test/Lower/OpenACC/acc-declare-use-associated.f90 (+1-1)
  • (modified) flang/test/Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90 (+2-2)
  • (modified) flang/test/Lower/OpenMP/cray-pointers01.f90 (+1-1)
  • (modified) flang/test/Lower/OpenMP/declare-target-data.f90 (+2-2)
  • (modified) flang/test/Lower/OpenMP/threadprivate-char-array-chararray.f90 (+2-2)
  • (modified) flang/test/Lower/OpenMP/wsloop-reduction-multiple-clauses.f90 (+1-1)
  • (modified) flang/test/Lower/array-character.f90 (+1-1)
  • (modified) flang/test/Lower/array-constructor-1.f90 (+1-1)
  • (modified) flang/test/Lower/array-constructor-2.f90 (+2-2)
  • (modified) flang/test/Lower/array.f90 (+8-8)
  • (modified) flang/test/Lower/constant-literal-mangling.f90 (+5-5)
  • (modified) flang/test/Lower/constant-logical-transfer.f90 (+1-1)
  • (modified) flang/test/Lower/convert.f90 (+1-1)
  • (modified) flang/test/Lower/default-initialization-globals.f90 (+6-6)
  • (modified) flang/test/Lower/dense-array-any-rank.f90 (+3-3)
  • (modified) flang/test/Lower/dense-attributed-array.f90 (+1-1)
  • (modified) flang/test/Lower/derived-type-descriptor.f90 (+2-2)
  • (modified) flang/test/Lower/equivalence-1.f90 (+1-1)
  • (modified) flang/test/Lower/equivalence-static-init.f90 (+2-2)
  • (added) flang/test/Lower/global-alignment.f90 (+74)
  • (modified) flang/test/Lower/io-derived-type.f90 (+2-2)
  • (modified) flang/test/Lower/module_definition.f90 (+3-3)
  • (modified) flang/test/Lower/module_use.f90 (+1-1)
  • (modified) flang/test/Lower/namelist-common-block.f90 (+1-1)
  • (added) m.mod (+16)
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 59abdb92e33ba..65aa8b95549ba 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -223,6 +223,9 @@ static fir::GlobalOp declareGlobal(Fortran::lower::AbstractConverter &converter,
   fir::GlobalOp global = builder.createGlobal(
       loc, converter.genType(var), globalName, linkage, mlir::Attribute{},
       isConstant(ultimate), var.isTarget(), dataAttr);
+  // BIND(C) globals follow C ABI alignment; remove default alignment.
+  if (sym.attrs().test(Fortran::semantics::Attr::BIND_C))
+    global.removeAlignmentAttr();
   attachAccDeclareAttribute(builder, global, sym);
   return global;
 }
@@ -566,6 +569,9 @@ fir::GlobalOp Fortran::lower::defineGlobal(
             oeDetails->init().value(), dataAttr);
         if (global) {
           global.setVisibility(mlir::SymbolTable::Visibility::Public);
+          // BIND(C) globals follow C ABI alignment; remove default alignment.
+          if (sym.attrs().test(Fortran::semantics::Attr::BIND_C))
+            global.removeAlignmentAttr();
           return global;
         }
       }
@@ -680,6 +686,9 @@ fir::GlobalOp Fortran::lower::defineGlobal(
   // Set public visibility to prevent global definition to be optimized out
   // even if they have no initializer and are unused in this compilation unit.
   global.setVisibility(mlir::SymbolTable::Visibility::Public);
+  // BIND(C) globals follow C ABI alignment; remove default alignment.
+  if (sym.attrs().test(Fortran::semantics::Attr::BIND_C))
+    global.removeAlignmentAttr();
   return global;
 }
 
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index 6a9c84ffbd909..5e64ce6a767c7 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -463,6 +463,12 @@ fir::GlobalOp fir::FirOpBuilder::createGlobal(
   }
   auto glob = fir::GlobalOp::create(*this, loc, name, isConst, isTarget, type,
                                     value, linkage, attrs);
+  // Set default alignment for array globals.
+  if (mlir::isa<fir::SequenceType>(type)) {
+    unsigned currentAlign = glob.getAlignment().value_or(0);
+    if (currentAlign < 64)
+      glob.setAlignment(64);
+  }
   restoreInsertionPoint(insertPt);
   if (symbolTable)
     symbolTable->insert(glob);
@@ -480,6 +486,12 @@ fir::GlobalOp fir::FirOpBuilder::createGlobal(
   setInsertionPoint(module.getBody(), module.getBody()->end());
   auto glob = fir::GlobalOp::create(*this, loc, name, isConst, isTarget, type,
                                     mlir::Attribute{}, linkage);
+  // Set default alignment for array globals.
+  if (mlir::isa<fir::SequenceType>(type)) {
+    unsigned currentAlign = glob.getAlignment().value_or(0);
+    if (currentAlign < 64)
+      glob.setAlignment(64);
+  }
   auto &region = glob.getRegion();
   region.push_back(new mlir::Block);
   auto &block = glob.getRegion().back();
diff --git a/flang/test/Lower/CUDA/cuda-data-attribute.cuf b/flang/test/Lower/CUDA/cuda-data-attribute.cuf
index 2b06f29af1ef0..a461ec2835996 100644
--- a/flang/test/Lower/CUDA/cuda-data-attribute.cuf
+++ b/flang/test/Lower/CUDA/cuda-data-attribute.cuf
@@ -117,7 +117,7 @@ end subroutine
 ! CHECK: fir.global @_QMcuda_varEmod_b_ra {data_attr = #cuf.cuda<device>} : f32
 ! CHECK: fir.global @_QMcuda_varEmod_c_rm {data_attr = #cuf.cuda<managed>} : !fir.box<!fir.heap<f32>>
 
-! CHECK: fir.global @_QMcuda_varEmod_d_i_init(dense<[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]> : tensor<10xi32>) {data_attr = #cuf.cuda<device>} : !fir.array<10xi32>
-! CHECK: fir.global @_QMcuda_varEmod_d_rinit(dense<[{{.*}}]> : tensor<10xf32>) {data_attr = #cuf.cuda<device>} : !fir.array<10xf32>
+! CHECK: fir.global @_QMcuda_varEmod_d_i_init(dense<[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]> : tensor<10xi32>) {alignment = 64 : i64, data_attr = #cuf.cuda<device>} : !fir.array<10xi32>
+! CHECK: fir.global @_QMcuda_varEmod_d_rinit(dense<[{{.*}}]> : tensor<10xf32>) {alignment = 64 : i64, data_attr = #cuf.cuda<device>} : !fir.array<10xf32>
 ! CHECK: fir.global @_QMcuda_varEmod_d_rp {data_attr = #cuf.cuda<pinned>} : !fir.box<!fir.heap<f32>>
-! CHECK: fir.global @_QMcuda_varEmod_d_t {data_attr = #cuf.cuda<device>} : !fir.array<2x!fir.type<_QMcuda_varTt1{a:i32}>>
+! CHECK: fir.global @_QMcuda_varEmod_d_t {alignment = 64 : i64, data_attr = #cuf.cuda<device>} : !fir.array<2x!fir.type<_QMcuda_varTt1{a:i32}>>
diff --git a/flang/test/Lower/CUDA/cuda-program-global.cuf b/flang/test/Lower/CUDA/cuda-program-global.cuf
index 64141f940a541..cdeb1a1446ecf 100644
--- a/flang/test/Lower/CUDA/cuda-program-global.cuf
+++ b/flang/test/Lower/CUDA/cuda-program-global.cuf
@@ -23,5 +23,5 @@ end
 ! CHECK: cuf.alloc !fir.array<10xi32> {bindc_name = "u", data_attr = #cuf.cuda<unified>, uniq_name = "_QFEu"} -> !fir.ref<!fir.array<10xi32>>
 
 ! CHECK-NOT: fir.global internal @_QFEa {data_attr = #cuf.cuda<device>} : !fir.array<10xi32> {{{$}}
-! CHECK: fir.global internal @_QFEb : !fir.array<10xi32> {{{$}}
+! CHECK: fir.global internal @_QFEb {alignment = 64 : i64} : !fir.array<10xi32> {{{$}}
 ! CHECK-NOT: fir.global internal @_QFEu {data_attr = #cuf.cuda<unified>}
diff --git a/flang/test/Lower/HLFIR/tdesc-character-comp-init.f90 b/flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
index 1ae312e8cc1c4..909523d19f702 100644
--- a/flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
+++ b/flang/test/Lower/HLFIR/tdesc-character-comp-init.f90
@@ -9,5 +9,5 @@ subroutine test()
   end type
   type(t) :: x
 end subroutine
-! CHECK-LABEL: fir.global {{.*}} @_QFtestE.c.t constant
+! CHECK-LABEL: fir.global {{.*}} @_QFtestE.c.t {alignment = 64 : i64} constant
 ! CHECK: fir.address_of(@_QFtestE.di.t.character_comp) : !fir.ref<!fir.char<1,5>>
diff --git a/flang/test/Lower/Intrinsics/ieee_class.f90 b/flang/test/Lower/Intrinsics/ieee_class.f90
index ab177e40e0d0d..1f59abb22122d 100644
--- a/flang/test/Lower/Intrinsics/ieee_class.f90
+++ b/flang/test/Lower/Intrinsics/ieee_class.f90
@@ -127,5 +127,5 @@ program p
   enddo
 end
 
-! CHECK: fir.global linkonce @_FortranAIeeeClassTable(dense<[7, 8, 8, 8, 11, 11, 11, 11, 9, 9, 9, 9, 10, 2, 1, 2, 6, 5, 5, 5, 11, 11, 11, 11, 4, 4, 4, 4, 3, 2, 1, 2]> : tensor<32xi8>) constant : !fir.array<32xi8>
-! CHECK: fir.global linkonce @_FortranAIeeeValueTable_8(dense<[0, 9219994337134247936, 9221120237041090560, -4503599627370496, -4616189618054758400, -9221120237041090560, -9223372036854775808, 0, 2251799813685248, 4607182418800017408, 9218868437227405312, 0]> : tensor<12xi64>) constant : !fir.array<12xi64>
+! CHECK: fir.global linkonce @_FortranAIeeeClassTable(dense<[7, 8, 8, 8, 11, 11, 11, 11, 9, 9, 9, 9, 10, 2, 1, 2, 6, 5, 5, 5, 11, 11, 11, 11, 4, 4, 4, 4, 3, 2, 1, 2]> : tensor<32xi8>) {alignment = 64 : i64} constant : !fir.array<32xi8>
+! CHECK: fir.global linkonce @_FortranAIeeeValueTable_8(dense<[0, 9219994337134247936, 9221120237041090560, -4503599627370496, -4616189618054758400, -9221120237041090560, -9223372036854775808, 0, 2251799813685248, 4607182418800017408, 9218868437227405312, 0]> : tensor<12xi64>) {alignment = 64 : i64} constant : !fir.array<12xi64>
diff --git a/flang/test/Lower/OpenACC/acc-declare-globals.f90 b/flang/test/Lower/OpenACC/acc-declare-globals.f90
index 4556c5f4ddb1c..94d9b00d73467 100644
--- a/flang/test/Lower/OpenACC/acc-declare-globals.f90
+++ b/flang/test/Lower/OpenACC/acc-declare-globals.f90
@@ -42,7 +42,7 @@ module acc_declare_test
  !$acc declare create(data1)
 end module
 
-! CHECK-LABEL: fir.global @_QMacc_declare_testEdata1 {acc.declare = #acc.declare<dataClause = acc_create>} : !fir.array<100000xf32>
+! CHECK-LABEL: fir.global @_QMacc_declare_testEdata1 {acc.declare = #acc.declare<dataClause = acc_create>, alignment = 64 : i64} : !fir.array<100000xf32>
 
 ! CHECK-LABEL: acc.global_ctor @_QMacc_declare_testEdata1_acc_ctor {
 ! CHECK:         %[[GLOBAL_ADDR:.*]] = fir.address_of(@_QMacc_declare_testEdata1) {acc.declare = #acc.declare<dataClause = acc_create>} : !fir.ref<!fir.array<100000xf32>>
@@ -86,7 +86,7 @@ module acc_declare_device_resident_test
  !$acc declare device_resident(data1)
 end module
 
-! CHECK-LABEL: fir.global @_QMacc_declare_device_resident_testEdata1 {acc.declare = #acc.declare<dataClause =  acc_declare_device_resident>} : !fir.array<5000xi32>
+! CHECK-LABEL: fir.global @_QMacc_declare_device_resident_testEdata1 {acc.declare = #acc.declare<dataClause =  acc_declare_device_resident>, alignment = 64 : i64} : !fir.array<5000xi32>
 
 ! CHECK-LABEL: acc.global_ctor @_QMacc_declare_device_resident_testEdata1_acc_ctor {
 ! CHECK:         %[[GLOBAL_ADDR:.*]] = fir.address_of(@_QMacc_declare_device_resident_testEdata1) {acc.declare = #acc.declare<dataClause =  acc_declare_device_resident>} : !fir.ref<!fir.array<5000xi32>>
@@ -109,7 +109,7 @@ module acc_declare_device_link_test
  !$acc declare link(data1)
 end module
 
-! CHECK-LABEL: fir.global @_QMacc_declare_device_link_testEdata1 {acc.declare = #acc.declare<dataClause =  acc_declare_link>} : !fir.array<5000xi32> {
+! CHECK-LABEL: fir.global @_QMacc_declare_device_link_testEdata1 {acc.declare = #acc.declare<dataClause =  acc_declare_link>, alignment = 64 : i64} : !fir.array<5000xi32> {
 
 ! CHECK-LABEL: acc.global_ctor @_QMacc_declare_device_link_testEdata1_acc_ctor {
 ! CHECK:         %[[GLOBAL_ADDR:.*]] = fir.address_of(@_QMacc_declare_device_link_testEdata1) {acc.declare = #acc.declare<dataClause =  acc_declare_link>} : !fir.ref<!fir.array<5000xi32>>
diff --git a/flang/test/Lower/OpenACC/acc-declare-use-associated.f90 b/flang/test/Lower/OpenACC/acc-declare-use-associated.f90
index cd700ae2932b2..4d2275d67efb5 100644
--- a/flang/test/Lower/OpenACC/acc-declare-use-associated.f90
+++ b/flang/test/Lower/OpenACC/acc-declare-use-associated.f90
@@ -25,5 +25,5 @@ subroutine use_mod()
   end do
 end subroutine
 
-! CHECK: fir.global @_QMacc_declare_modEaa {acc.declare = #acc.declare<dataClause = acc_create>} : !fir.array<100xf32>
+! CHECK: fir.global @_QMacc_declare_modEaa {acc.declare = #acc.declare<dataClause = acc_create>, alignment = 64 : i64} : !fir.array<100xf32>
 ! CHECK: fir.global @_QMacc_declare_modEcoef {acc.declare = #acc.declare<dataClause = acc_copyin>} : f32
diff --git a/flang/test/Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90 b/flang/test/Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90
index 676686f6a2def..3f1ed7842f560 100644
--- a/flang/test/Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90
+++ b/flang/test/Lower/OpenMP/DelayedPrivatization/target-private-implicit-scalar-map-2.f90
@@ -14,8 +14,8 @@ module test_data
 end module
 
 ! CHECK-MOD: module {{.*}}
-! CHECK-MOD: fir.global @_QMtest_dataEj : !fir.array<200xi8> {
-! CHECK-MOD: fir.global @_QMtest_dataEi : !fir.array<10x10xf32> {
+! CHECK-MOD: fir.global @_QMtest_dataEj {alignment = 64 : i64} : !fir.array<200xi8> {
+! CHECK-MOD: fir.global @_QMtest_dataEi {alignment = 64 : i64} : !fir.array<10x10xf32> {
 ! CHECK-MOD: fir.global @_QMtest_dataEz : i32 {
 
 !--- imp_scalar_map_target.f90
diff --git a/flang/test/Lower/OpenMP/cray-pointers01.f90 b/flang/test/Lower/OpenMP/cray-pointers01.f90
index 01fa4af3282b1..5f46ae5608c0a 100644
--- a/flang/test/Lower/OpenMP/cray-pointers01.f90
+++ b/flang/test/Lower/OpenMP/cray-pointers01.f90
@@ -56,7 +56,7 @@ program test_cray_pointers_01
     ! CHECK:   omp.terminator
     ! CHECK: }
     ! CHECK-LABEL: fir.global @_QMtest_host_assoc_cray_pointerEivar : i64
-    ! CHECK-LABEL: fir.global  @_QMtest_host_assoc_cray_pointerEvar : !fir.array<?xf64>
+    ! CHECK-LABEL: fir.global  @_QMtest_host_assoc_cray_pointerEvar {alignment = 64 : i64} : !fir.array<?xf64>
 
 
   !$omp end parallel
diff --git a/flang/test/Lower/OpenMP/declare-target-data.f90 b/flang/test/Lower/OpenMP/declare-target-data.f90
index 474944d7c0bb0..2e3790303984c 100644
--- a/flang/test/Lower/OpenMP/declare-target-data.f90
+++ b/flang/test/Lower/OpenMP/declare-target-data.f90
@@ -8,11 +8,11 @@ module test_0
 INTEGER :: data_int = 10
 !$omp declare target link(data_int)
 
-!CHECK-DAG: fir.global @_QMtest_0Earray_1d({{.*}}) {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (link), automap = false>} : !fir.array<3xi32>
+!CHECK-DAG: fir.global @_QMtest_0Earray_1d({{.*}}) {alignment = 64 : i64, omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (link), automap = false>} : !fir.array<3xi32>
 INTEGER :: array_1d(3) = (/1,2,3/)
 !$omp declare target link(array_1d)
 
-!CHECK-DAG: fir.global @_QMtest_0Earray_2d({{.*}}) {omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (link), automap = false>} : !fir.array<2x2xi32>
+!CHECK-DAG: fir.global @_QMtest_0Earray_2d({{.*}}) {alignment = 64 : i64, omp.declare_target = #omp.declaretarget<device_type = (any), capture_clause = (link), automap = false>} : !fir.array<2x2xi32>
 INTEGER :: array_2d(2,2) = reshape((/1,2,3,4/), (/2,2/))
 !$omp declare target link(array_2d)
 
diff --git a/flang/test/Lower/OpenMP/threadprivate-char-array-chararray.f90 b/flang/test/Lower/OpenMP/threadprivate-char-array-chararray.f90
index 34b21150bb886..4be86bfe7f0a7 100644
--- a/flang/test/Lower/OpenMP/threadprivate-char-array-chararray.f90
+++ b/flang/test/Lower/OpenMP/threadprivate-char-array-chararray.f90
@@ -11,8 +11,8 @@ module test
   !$omp threadprivate(x, y, z)
 
 !CHECK-DAG: fir.global @_QMtestEx : !fir.char<1> {
-!CHECK-DAG: fir.global @_QMtestEy : !fir.array<5xi32> {
-!CHECK-DAG: fir.global @_QMtestEz : !fir.array<5x!fir.char<1,5>> {
+!CHECK-DAG: fir.global @_QMtestEy {alignment = 64 : i64} : !fir.array<5xi32> {
+!CHECK-DAG: fir.global @_QMtestEz {alignment = 64 : i64} : !fir.array<5x!fir.char<1,5>> {
 
 contains
   subroutine sub()
diff --git a/flang/test/Lower/OpenMP/wsloop-reduction-multiple-clauses.f90 b/flang/test/Lower/OpenMP/wsloop-reduction-multiple-clauses.f90
index 60a162d8f8002..bd3926647c759 100644
--- a/flang/test/Lower/OpenMP/wsloop-reduction-multiple-clauses.f90
+++ b/flang/test/Lower/OpenMP/wsloop-reduction-multiple-clauses.f90
@@ -155,7 +155,7 @@ program main
 ! CHECK:             omp.terminator
 ! CHECK:           }
 
-! CHECK-LABEL:   fir.global internal @_QFEarray : !fir.array<3x3xf64> {
+! CHECK-LABEL:   fir.global internal @_QFEarray {alignment = 64 : i64} : !fir.array<3x3xf64> {
 ! CHECK:           %[[VAL_0:.*]] = fir.zero_bits !fir.array<3x3xf64>
 ! CHECK:           fir.has_value %[[VAL_0]] : !fir.array<3x3xf64>
 ! CHECK:         }
diff --git a/flang/test/Lower/array-character.f90 b/flang/test/Lower/array-character.f90
index 85f5af0492c3b..7c4c84a1ba7a1 100644
--- a/flang/test/Lower/array-character.f90
+++ b/flang/test/Lower/array-character.f90
@@ -109,7 +109,7 @@ subroutine charlit
 ! CHECK:           return
 ! CHECK:         }
 
-! CHECK: fir.global internal @_QQro.4x3xc1.0 constant : !fir.array<4x!fir.char<1,3>>
+! CHECK: fir.global internal @_QQro.4x3xc1.0 {alignment = 64 : i64} constant : !fir.array<4x!fir.char<1,3>>
 ! CHECK: AA
 ! CHECK: MM
 ! CHECK: ZZ
diff --git a/flang/test/Lower/array-constructor-1.f90 b/flang/test/Lower/array-constructor-1.f90
index 55e7bb0f0416b..19461006fde19 100644
--- a/flang/test/Lower/array-constructor-1.f90
+++ b/flang/test/Lower/array-constructor-1.f90
@@ -41,6 +41,6 @@ program prog
   call zero
 end
 
-! CHECK: fir.global internal @_QFzeroECa constant : !fir.array<0xcomplex<f32>>
+! CHECK: fir.global internal @_QFzeroECa {alignment = 64 : i64} constant : !fir.array<0xcomplex<f32>>
 ! CHECK:   %0 = fir.undefined !fir.array<0xcomplex<f32>>
 ! CHECK:   fir.has_value %0 : !fir.array<0xcomplex<f32>>
diff --git a/flang/test/Lower/array-constructor-2.f90 b/flang/test/Lower/array-constructor-2.f90
index e08fd111b8649..e4de5312f8f5c 100644
--- a/flang/test/Lower/array-constructor-2.f90
+++ b/flang/test/Lower/array-constructor-2.f90
@@ -153,6 +153,6 @@ subroutine test7(a, n)
   a = (/ (CHAR(i), i=1,n) /)
 end subroutine test7
 
-! CHECK: fir.global internal @_QQro.3xr4.0(dense<[1.000000e+00, 2.000000e+00, 3.000000e+00]> : tensor<3xf32>) constant : !fir.array<3xf32>
+! CHECK: fir.global internal @_QQro.3xr4.0(dense<[1.000000e+00, 2.000000e+00, 3.000000e+00]> : tensor<3xf32>) {alignment = 64 : i64} constant : !fir.array<3xf32>
 
-! CHECK: fir.global internal @_QQro.4xi4.1(dense<[6, 7, 42, 9]> : tensor<4xi32>) constant : !fir.array<4xi32>
+! CHECK: fir.global internal @_QQro.4xi4.1(dense<[6, 7, 42, 9]> : tensor<4xi32>) {alignment = 64 : i64} constant : !fir.array<4xi32>
diff --git a/flang/test/Lower/array.f90 b/flang/test/Lower/array.f90
index f2dfbea9a084f..9a295a381f738 100644
--- a/flang/test/Lower/array.f90
+++ b/flang/test/Lower/array.f90
@@ -151,23 +151,23 @@ end subroutine hugeGlobal
 end
 
 ! c1 data
-! CHECK: fir.global internal @_QFrangeEc1(dense<(0.000000e+00,0.000000e+00)> : tensor<3x2xcomplex<f32>>) : !fir.array<2x3xcomplex<f32>>
+! CHECK: fir.global internal @_QFrangeEc1(dense<(0.000000e+00,0.000000e+00)> : tensor<3x2xcomplex<f32>>) {alignment = 64 : i64} : !fir.array<2x3xcomplex<f32>>
 
 ! a0 array constructor
-! CHECK: fir.global internal @_QQro.10xi4.{{.*}}(dense<[1, 2, 3, 3, 3, 3, 3, 3, 3, 3]> : tensor<10xi32>) constant : !fir.array<10xi32>
+! CHECK: fir.global internal @_QQro.10xi4.{{.*}}(dense<[1, 2, 3, 3, 3, 3, 3, 3, 3, 3]> : tensor<10xi32>) {alignment = 64 : i64} constant : !fir.array<10xi32>
 
 ! a1 array constructor
-! CHECK: fir.global internal @_QQro.2x3xr4.{{.*}}(dense<3.500000e+00> : tensor<3x2xf32>) constant : !fir.array<2x3xf32>
+! CHECK: fir.global internal @_QQro.2x3xr4.{{.*}}(dense<3.500000e+00> : tensor<3x2xf32>) {alignment = 64 : i64} constant : !fir.array<2x3xf32>
 
 ! a2 array constructor
-! CHECK: fir.global internal @_QQro.3x4xi4.{{.*}}(dense<{{\[\[1, 3, 3], \[5, 3, 3], \[3, 3, 9], \[9, 9, 8]]}}> : tensor<4x3xi32>) constant : !fir.array<3x4xi32>
+! CHECK: fir.global internal @_QQro.3x4xi4.{{.*}}(dense<{{\[\[1, 3, 3], \[5, 3, 3], \[3, 3, 9], \[9, 9, 8]]}}> : tensor<4x3xi32>) {alignment = 64 : i64} constant : !fir.array<3x4xi32>
 
 ! a3 array constructor
-! CHECK: fir.global internal @_QQro.2x3x4xi4.{{.*}}(dense<{{\[\[\[1, 1], \[2, 2], \[3, 3]], \[\[4, 4], \[5, 5], \[6, 6]], \[\[7, 7], \[8, 8], \[9, 9]], \[\[10, 10], \[11, 11], \[12, 12]]]}}> : tensor<4x3x2xi32>) constant : !fir.array<2x3x4xi32>
+! CHECK: fir.global internal @_QQro.2x3x4xi4.{{.*}}(dense<{{\[\[\[1, 1], \[2, 2], \[3, 3]], \[\[4, 4], \[5, 5], \[6, 6]], \[\[7, 7], \[8, 8], \[9, 9]], \[\[10, 10], \[11, 11], \[12, 12]]]}}> : tensor<4x3x2xi32>) {alignment = 64 : i64} constant : !fir.array<2x3x4xi32>
 
 ! c0 array constructor
-! CHECK: fir.global internal @_QQro.2x3xz4.{{.*}}(dense<{{\[}}[(1.000000e+00,1.500000e+00), (2.000000e+00,2.500000e+00)], [(3.000000e+00,3.500000e+00), (4.000000e+00,4.500000e+00)], [(5.000000e+00,5.500000e+00), (6.000000e+00,6.500000e+00)]]> : tensor<3x2xcomplex<f32>>) constant : !fir.array<2x3xcomplex<f32>>
+! CHECK: fir.global internal @_QQro.2x3xz4.{{.*}}(dense<{{\[}}[(1.000000e+00,1.500000e+00), (2.000000e+00,2.500000e+00)], [(3.000000e+00,3.500000e+00), (4.000000e+00,4.500000e+00)], [(5.000000e+00,5.500000e+00), (6.000000e+00,6.500000e+00)]]> : tensor<3x2xcomplex<f32>>) {alignment = 64 : i64} constant : !fir.array<2x3xcomplex<f32>>
 
-! CHECK: fir.global internal @_QFrangeglobal{{.*}}(dense<[1, 1, 2, 2, 3, 3]> : tensor<6xi32>) : !fir.array<6xi32>
+! CHECK: fir.global internal @_QFrangeglobal{{.*}}(dense<[1, 1, 2, 2, 3, 3]> : tensor<6xi32>) {alignment = 64 : i64} : !fir.array<6xi32>
 
-! CHECK: fir.global internal @_QQro.500x500xi4.{{.*}}(dense<{{.*}}> : tensor<500x500xi32>) constant : !fir.array<500x500xi32>
+! CHECK: fir.global internal @_QQro.500x500xi4.{{.*}}(dense<{{.*}}> : tensor<500x500xi32>) {alignment = 64 : i64} constant : !fir.array<500x500xi32>
diff --git a/flang/test/Lower/constant-literal-mangling.f90 b/flang/test/Lower/constant-literal-mangling.f90
index 6ef1367369a30..6f72a86a7859a 100644
--- a/flang/test/Lower/constant-literal-mangling.f90
+++ b/flang/test/Lower/constant-literal-mangling.f90
@@ -78,24 +78,24 @@
   print *, [emptyType2()]
 end
 
-! CHECK: fir.global internal @_QQro.1x_QFTsometype.10 constant : !fir.array<1x!fir.type<_QFTsometype{i:i32}>> {
+! CHECK: fir.global internal @_QQro.1x_QFTsometype.10 {alignment = 64 : i64} constant : !fir.array<1x!fir.type<_QFTsometype{i:i32}>> {
 ! CHECK:   %{{.*}} = arith.constant 11 : i32
 ! CHECK: }
 
-! CHECK: fi...
[truncated]

Comment thread m.mod
@@ -0,0 +1,16 @@
!mod$ v1 sum:444ba0b36254725f
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like an accidental git add

Copy link
Copy Markdown
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks for working on this.

module m
implicit none

! Array Globals that should get alignment 64 by default.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
! Array Globals that should get alignment 64 by default.
! Array globals that should get alignment 64 by default.

end block data

! CHECK: fir.global @initblock_ {alignment = 4 : i64} : tuple<!fir.array<5xi32>>
! CHECK-NOT: alignment = 64
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't follow why these CHECK lines are here. Is there something that comes between @initblock_ and @myblock_ that is expected to have an alignment? Or are they intended to say that @initblock_ and @myblock_ are not 64-byte aligned?

If the former, there should probably be CHECK line here for the entity that is not 64-byte aligned. If the latter, then these are probably unnecessary since the previous line already checks for alignment = 4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just a nit, but in general, it would help to keep the check lines closer to the code that they correspond to. It was a bit difficult to review things having to keep going up and down. There are no firm rules on this, and it doesn't always make sense, but it might have helped here :-)

Comment on lines +466 to +471
// Set default alignment for array globals.
if (mlir::isa<fir::SequenceType>(type)) {
unsigned currentAlign = glob.getAlignment().value_or(0);
if (currentAlign < 64)
glob.setAlignment(64);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks like it overrides the alignment on glob. Is there a chance that glob.getAlignment() returns, say 16. In that case, is it correct to override it?

// Set default alignment for array globals.
if (mlir::isa<fir::SequenceType>(type)) {
unsigned currentAlign = glob.getAlignment().value_or(0);
if (currentAlign < 64)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think it would be worthwhile to have a default alignment constant declared somewhere and then use that instead of hardcoding the value 64 here? Is this something that we might change in the future? Or, could this be used elsewhere in the compiler?

@tarunprabhu tarunprabhu requested a review from tblah April 29, 2026 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang:openmp flang Flang issues not falling into any other category openacc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants