Fix https://github.com/llvm/llvm-project/issues/63579
% cat a.c void foo() {} % clang --target=arm-none-eabi -mthumb -mno-unaligned-access -fsanitize=kcfi a.c -S -o - | grep p2align .p2align 1 % clang --target=armv6m-none-eabi -fsanitize=function a.c -S -o - | grep p2align .p2align 1
With -mno-unaligned-access (possibly implicit), we should ensure that
-fsanitize={function,kcfi} instrumented functions are aligned by at least 4, so
that loading the type hash before the function label will not cause a misaligned
access, even if the backend doesn't set setMinFunctionAlignment to 4 or greater.
With this patch, the generated assembly for the examples above will contain .p2align 2.
If -falign-functions= is specified, take the maxiumum.
If __attribute__((aligned(2))) is specified, arbitrarily let the function
attribute win.
Since SanOpts is per-function, move the alignment setting code from
CodeGenModule::SetLLVMFunctionAttributesForDefinition to CodeGenFunction.
This move requires some attention.
Note: CodeGenModule::SetLLVMFunctionAttributesForDefinition is called by many
thunk codegen code with a dummy GlobalDecl/FunctionDecl.
However, in one call site, MicrosoftCXXABI::EmitVirtualMemPtrThunk has a
SetLLVMFunctionAttributesForDefinition use case that requires the
"Some C++ ABIs require 2-byte alignment for member functions" code. So
keep this part in CodeGenModule.