Skip to content

Commit

Permalink
[RISCV] Support -m[no-]strict-align options
Browse files Browse the repository at this point in the history
To match GCC.

Options `-m[no-]strict-align` are aliases of `-m[no-]unaligned-access`
in clang, but there is no corresponding option in GCC.

Support of `-m[no-]unaligned-access` in GCC may be needed to align
Clang/GCC.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D155456
  • Loading branch information
wangpc-pp committed Aug 3, 2023
1 parent 4e814b1 commit 23ce536
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ LoongArch Support

RISC-V Support
^^^^^^^^^^^^^^
- Unaligned memory accesses can be toggled by ``-m[no-]unaligned-access`` or the
aliases ``-m[no-]strict-align``.

CUDA/HIP Language Changes
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3834,9 +3834,9 @@ def mrvv_vector_bits_EQ : Joined<["-"], "mrvv-vector-bits=">, Group<m_Group>,
"in __riscv_v_fixed_vlen preprocessor define (RISC-V only)">;

def munaligned_access : Flag<["-"], "munaligned-access">, Group<m_Group>,
HelpText<"Allow memory accesses to be unaligned (AArch32/AArch64/LoongArch only)">;
HelpText<"Allow memory accesses to be unaligned (AArch32/AArch64/LoongArch/RISC-V only)">;
def mno_unaligned_access : Flag<["-"], "mno-unaligned-access">, Group<m_Group>,
HelpText<"Force all memory accesses to be aligned (AArch32/AArch64/LoongArch only)">;
HelpText<"Force all memory accesses to be aligned (AArch32/AArch64/LoongArch/RISC-V only)">;
} // let Flags = [TargetSpecific]
def mstrict_align : Flag<["-"], "mstrict-align">, Alias<mno_unaligned_access>, Flags<[CC1Option,HelpHidden]>,
HelpText<"Force all memory accesses to be aligned (same as mno-unaligned-access)">;
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/Driver/ToolChains/Arch/RISCV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ void riscv::getRISCVTargetFeatures(const Driver &D, const llvm::Triple &Triple,
else
Features.push_back("-save-restore");

// -mno-unaligned-access is default, unless -munaligned-access is specified.
bool HasV = llvm::is_contained(Features, "+zve32x");
if (Args.hasFlag(options::OPT_munaligned_access,
options::OPT_mno_unaligned_access, false)) {
Features.push_back("+unaligned-scalar-mem");
if (HasV)
Features.push_back("+unaligned-vector-mem");
} else {
Features.push_back("-unaligned-scalar-mem");
if (HasV)
Features.push_back("-unaligned-vector-mem");
}

// Now add any that the user explicitly requested on the command line,
// which may override the defaults.
handleTargetFeaturesGroup(D, Triple, Args, Features,
Expand Down
2 changes: 2 additions & 0 deletions clang/test/Driver/riscv-default-features.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

// RV32: "target-features"="+32bit,+a,+c,+m,+relax,
// RV32-SAME: -save-restore
// RV32-SAME: -unaligned-scalar-mem
// RV64: "target-features"="+64bit,+a,+c,+m,+relax,
// RV64-SAME: -save-restore
// RV64-SAME: -unaligned-scalar-mem

// Dummy function
int foo(void){
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Driver/riscv-features.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
// DEFAULT: "-target-feature" "-save-restore"
// DEFAULT-NOT: "-target-feature" "+save-restore"

// RUN: %clang --target=riscv32-unknown-elf -### %s -munaligned-access 2>&1 | FileCheck %s -check-prefix=UNALIGNED-SCALAR-MEM
// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-unaligned-access 2>&1 | FileCheck %s -check-prefix=NO-UNALIGNED-SCALAR-MEM
// RUN: %clang --target=riscv32-unknown-elf -### %s -mno-strict-align 2>&1 | FileCheck %s -check-prefix=UNALIGNED-SCALAR-MEM
// RUN: %clang --target=riscv32-unknown-elf -### %s -mstrict-align 2>&1 | FileCheck %s -check-prefix=NO-UNALIGNED-SCALAR-MEM
// RUN: %clang --target=riscv32-unknown-elf -### %s -march=rv32gv -munaligned-access 2>&1 | FileCheck %s -check-prefix=UNALIGNED-VECTOR-MEM
// RUN: %clang --target=riscv32-unknown-elf -### %s -march=rv32gv -mno-unaligned-access 2>&1 | FileCheck %s -check-prefix=NO-UNALIGNED-VECTOR-MEM
// RUN: %clang --target=riscv32-unknown-elf -### %s -march=rv32gv -mno-strict-align 2>&1 | FileCheck %s -check-prefix=UNALIGNED-VECTOR-MEM
// RUN: %clang --target=riscv32-unknown-elf -### %s -march=rv32gv -mstrict-align 2>&1 | FileCheck %s -check-prefix=NO-UNALIGNED-VECTOR-MEM

// UNALIGNED-SCALAR-MEM: "-target-feature" "+unaligned-scalar-mem"
// NO-UNALIGNED-SCALAR-MEM: "-target-feature" "-unaligned-scalar-mem"
// UNALIGNED-VECTOR-MEM: "-target-feature" "+unaligned-vector-mem"
// NO-UNALIGNED-VECTOR-MEM: "-target-feature" "-unaligned-vector-mem"
// DEFAULT: "-target-feature" "-unaligned-scalar-mem"
// DEFAULT-NOT: "-target-feature" "+unaligned-scalar-mem"

// RUN: %clang --target=riscv32-linux -### %s -fsyntax-only 2>&1 \
// RUN: | FileCheck %s -check-prefix=DEFAULT-LINUX
// RUN: %clang --target=riscv64-linux -### %s -fsyntax-only 2>&1 \
Expand Down

0 comments on commit 23ce536

Please sign in to comment.