This is an archive of the discontinued LLVM Phabricator instance.

[LoongArch] Add support for ISD::FRAMEADDR and ISD::RETURNADDR
ClosedPublic

Authored by gonglingqin on Oct 18 2022, 6:02 PM.

Details

Summary

For now, only support lowering frame/return address for current frame.

Diff Detail

Event Timeline

gonglingqin created this revision.Oct 18 2022, 6:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 18 2022, 6:02 PM
gonglingqin requested review of this revision.Oct 18 2022, 6:02 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 18 2022, 6:02 PM
xen0n added inline comments.Oct 18 2022, 7:19 PM
llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
241–246

Why this limitation? I can't seem to find similar guard in AArch64, CSKY, RISCV or VE, but it is indeed present for MIPS and SystemZ.

llvm/test/CodeGen/LoongArch/frameaddr-returnaddr.ll
7

Please convert to opaque pointers throughout.

gonglingqin added inline comments.Oct 18 2022, 7:56 PM
llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
241–246

Good question! When the depth!=0 and the caller function does not use FP to save the current stack frame, causing an illegal address call, as in the following example:

#include <stdio.h>

void __attribute__((noinline)) foo() {

  printf("foo\n");
  printf("return address: %p\n", __builtin_return_address(2));
}
void __attribute__((noinline)) bar() {

  foo();
  printf("bar\n");
  printf("return address: %p\n", __builtin_return_address(0));
}
int main() {

  bar();
  return 0;
}

Compiling this test case with O2 on riscv64 reproduces this problem.
In reference to 'https://llvm.org/docs/LangRef.html#llvm-frameaddress-intrinsic' and 'https://llvm.org/docs/LangRef.html#llvm-returnaddress', this limitation is increased and returns 0 if depth is not equal to 0.

llvm/test/CodeGen/LoongArch/frameaddr-returnaddr.ll
7

Thanks, I will modify it.

Address @xen0n's comments.

This revision is now accepted and ready to land.Oct 21 2022, 2:36 AM
SixWeining added inline comments.Oct 23 2022, 6:36 PM
llvm/test/CodeGen/LoongArch/frameaddr-returnaddr.ll
51

For an assertion build, this will crash.

SixWeining added inline comments.Oct 23 2022, 7:04 PM
llvm/test/CodeGen/LoongArch/frameaddr-returnaddr.ll
51

Maybe this test can be removed before ; REQUIRES: noasserts getting supported by llvm-lit?

gonglingqin added inline comments.Oct 23 2022, 7:30 PM
llvm/test/CodeGen/LoongArch/frameaddr-returnaddr.ll
51

Thanks, I will remove it.

Remove test cases test_frameaddress_2() and test_returnaddress_2()

SixWeining added inline comments.Oct 23 2022, 10:48 PM
llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll
7

The _0 suffix is needless as you don't have _1 or _2.

SixWeining added inline comments.Oct 23 2022, 11:02 PM
llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll
7

Perhaps non_const_depth_frameaddress?

gonglingqin added inline comments.Oct 23 2022, 11:13 PM
llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll
7

Thanks, I will modify it.

Rename the test case

This revision was landed with ongoing or failed builds.Oct 24 2022, 12:13 AM
This revision was automatically updated to reflect the committed changes.

Seems that depth > 0 should also be supported (but not guarantee correctness). See D137541.