This is an archive of the discontinued LLVM Phabricator instance.

[sanitizer][msan] VarArgHelper for loongarch64
ClosedPublic

Authored by Ami-zhang on Aug 23 2023, 12:55 AM.

Details

Summary

This patch adds support for variadic argument for loongarch64,
which is based on MIPS64. And check-msan all pass.

Diff Detail

Event Timeline

Ami-zhang created this revision.Aug 23 2023, 12:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2023, 12:55 AM
Ami-zhang requested review of this revision.Aug 23 2023, 12:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2023, 12:55 AM
vitalybuka added inline comments.Sep 5 2023, 7:40 PM
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
5840

It looks like exact clone of Mips64 helper.
Only if (TargetTriple.getArch() == Triple::mips64) { is missing

Why not just use Mips64 then?

Ami-zhang added inline comments.Sep 6 2023, 3:06 AM
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
5840

Yes, the VarArg convention in LoongArch's ABI is similar to that of Mips, so VarArgLoongArch64Helper is here based on Mips.

If use Mips64 helper, like this:

+  else if (TargetTriple.isMIPS64() || TargetTriple.isLoongArch64())
     return new VarArgMIPS64Helper(Func, Msan, Visitor);

VarArgMIPS64Helper is a MIPS64-specific implementation. Adding loongarch64 here, does it seem somewhat ambiguous, suggesting LoongArch as a variant of MIPS architecture? I don't know if it's good to use it directly like mentioned above. So is it okay like above or have any better suggestions?

Thanks for your time.

vitalybuka added inline comments.Sep 6 2023, 10:38 AM
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
5840

How about something line this:

// Loongarch64 is not a MIPS, but the current vargs calling convention matches the MIPS.
using VarArgLoongArch64Helper = VarArgMIPS64Helper;

Another alternative is to rename VarArgMIPS64Helper into something more general, maybe there is a name for this calling convention already?

llvm/test/Instrumentation/MemorySanitizer/LoongArch/vararg-loongarch64.ll
4

even if we use MIPS helper, please keep this new test, in case someone modify Mips part ignoring loongarch64

xen0n added a comment.Sep 6 2023, 11:55 AM

I'm not looking at this in more detail right now (it's 3 am here), but isn't the LoongArch psABI mostly resembling RISCV, and not Mips?

I'm not looking at this in more detail right now (it's 3 am here), but isn't the LoongArch psABI mostly resembling RISCV, and not Mips?

I have read varargs part in the RISCV psABI at https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#va_list-va_start-and-va_arg and in Mips "elf64-2.4.pdf". That of LoongArch psABI is indeed very similar to that of RISCV, and also similar to MIPS.

RISCV is not yet supported in the MSan vararg instrumentation, so i referred to the implementation based on Mips here. After testing and verifying, the Mips-based vararg implementation in LoongArch works fine.

The ABI similarities between architectures could be coincidental, therefore we can't ignore the potential risks of sharing code. I should pay attention and update it accordingly if there are any changes in the future.

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
5840

Thanks for your suggestions. I'm inclined towards the first alternative.

As for renaming VarArgMIPS64Helper, we did consider renaming it to something more generic, but we haven't found a better name yet, and it's worth taking some time to research and find a suitable name. So for now, I will go with the first option.

Ami-zhang updated this revision to Diff 556224.Sep 7 2023, 8:32 PM
Ami-zhang edited the summary of this revision. (Show Details)

Use alias approach to implement VarArgLoongArch64Helper

Herald added a project: Restricted Project. · View Herald TranscriptSep 7 2023, 8:32 PM
Herald added subscribers: Restricted Project, arichardson, sdardis. · View Herald Transcript
fmayer added a subscriber: fmayer.Sep 8 2023, 11:30 AM
fmayer added inline comments.
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
5840

Please add a comment to the VarArgMIPS64Helper definition that explains that this is also used for Loongarch.

vitalybuka accepted this revision.Sep 8 2023, 11:33 AM

LGTM with @fmayer request

This revision is now accepted and ready to land.Sep 8 2023, 11:33 AM
Ami-zhang updated this revision to Diff 556386.Sep 10 2023, 8:08 PM

Add a comment

This revision was landed with ongoing or failed builds.Sep 11 2023, 6:52 PM
This revision was automatically updated to reflect the committed changes.