<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56110>56110</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
side effect with option "-mattr=+unaligned-scalar-mem"
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
BaoshanPang
</td>
</tr>
</table>
<pre>
For such ll code:
```
$ cat x.ll
; ModuleID = 'x.c'
source_filename = "x.c"
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32-unknown-unknown-elf"
; Function Attrs: argmemonly mustprogress nofree norecurse nosync nounwind willreturn
define dso_local void @foo_set(ptr nocapture noundef %a, i32 noundef %v) local_unnamed_addr #0 {
entry:
%bf.load = load i96, ptr %a, align 1
%0 = and i32 %v, 65535
%bf.value = zext i32 %0 to i96
%bf.shl = shl nuw nsw i96 %bf.value, 24
%bf.clear = and i96 %bf.load, -1099494850561
%bf.set = or i96 %bf.clear, %bf.shl
store i96 %bf.set, ptr %a, align 1
ret void
}
attributes #0 = { argmemonly mustprogress nofree norecurse nosync nounwind willreturn "frame-pointer"="none" "min-legal-vector-width"="0" "no-trapping-math"="true"
"stack-protector-buffer-size"="8" "target-features"="+a,+c,+m,+relax,-save-restore" }
!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 1, !"target-abi", !"ilp32"}
!2 = !{i32 1, !"SmallDataLimit", i32 8}
!3 = !{!"clang version 15.0.0 (git@172.30.80.200:/opt/repos/fortirisc_llvm.git c2bb2e5973acd24c45c1823e95e8e33003c59484)"}
```
When compile it with "-mattr=+unaligned-scalar-mem":
`$ ./bin/llc x.ll -O2 -mattr=+unaligned-scalar-mem`
We would get such asm code:
```
$ cat x.s
.text
.attribute 4, 16
.attribute 5, "rv32i2p0"
.file "x.c"
.globl foo_set # -- Begin function foo_set
.p2align 1
.type foo_set,@function
foo_set: # @foo_set
# %bb.0: # %entry
lw a2, 4(a0)
lw a3, 0(a0)
slli a4, a1, 16
srli a6, a4, 24
slli a1, a1, 24
lui a5, 4080
and a5, a5, a3
andi a4, a2, -256
or a1, a1, a5
or a4, a4, a6
sh a3, 0(a0)
srli a2, a2, 16
sh a2, 6(a0)
sh a4, 4(a0)
srli a1, a1, 16
sh a1, 2(a0)
ret
.Lfunc_end0:
.size foo_set, .Lfunc_end0-foo_set
# -- End function
.ident "clang version 15.0.0 (git@172.30.80.200:/opt/repos/fortirisc_llvm.git c2bb2e5973acd24c45c1823e95e8e33003c59484)"
.section ".note.GNU-stack","",@progbits
```
But if compile it **without** "-mattr=+unaligned-scalar-mem", the generated asm code is much shorter:
```
$ ./bin/llc x.ll -O2
[bpang@server220-256gb-3-4ghz-40 ~/lwork/llvm_fort/build]
$ cat x.s
.text
.attribute 4, 16
.attribute 5, "rv32i2p0"
.file "x.c"
.globl foo_set # -- Begin function foo_set
.p2align 1
.type foo_set,@function
foo_set: # @foo_set
# %bb.0: # %entry
srli a2, a1, 8
sb a1, 3(a0)
sb a2, 4(a0)
ret
.Lfunc_end0:
.size foo_set, .Lfunc_end0-foo_set
# -- End function
.ident "clang version 15.0.0 (git@172.30.80.200:/opt/repos/fortirisc_llvm.git c2bb2e5973acd24c45c1823e95e8e33003c59484)"
.section ".note.GNU-stack","",@progbits
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztV0mPozgU_jXJxQIZAwk55FDpdI1G6lmk1qiPkTEmeNoYZJukqn_9PJslkKrqqcNo1IeOqjDw9sXfM3lTPO8fG41MxyokJWJNwVfxwwofV_hhtcHDX_9IEsSoRU-hlMOb-IB-a4pO8l-PaBXDP9k-hQyuPd00nWb8VArJFa35wEI8C-lZLNVnblFBLZX0uensyMSDGhzhQQvXmPhLIDYJ3GySQMHD54hk92qsFq2c7Ghh2AU4O_VVNVc1rVyWk-AtkMdOMSsah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