[PATCH] D99237: [AVR][clang] Fix wrong calling convention in functions return struct type

Ben Shi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 28 21:29:56 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rGc94c8d8b5d99: [AVR][clang] Fix wrong calling convention in functions return struct type (authored by benshi001).

Changed prior to commit:
  https://reviews.llvm.org/D99237?vs=335368&id=355111#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99237/new/

https://reviews.llvm.org/D99237

Files:
  clang/lib/CodeGen/TargetInfo.cpp
  clang/test/CodeGen/avr/struct.c


Index: clang/test/CodeGen/avr/struct.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/avr/struct.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple avr -emit-llvm %s -o - | FileCheck %s
+
+// Structure that is more than 8 bytes.
+struct s10 {
+  int a, b, c, d, e;
+};
+
+// Structure that is less than 8 bytes.
+struct s06 {
+  int a, b, c;
+};
+
+struct s10 foo10(int a, int b, int c) {
+  struct s10 a0;
+  return a0;
+}
+
+struct s06 foo06(int a, int b, int c) {
+  struct s06 a0;
+  return a0;
+}
+
+// CHECK: %struct.s10 = type { i16, i16, i16, i16, i16 }
+// CHECK: %struct.s06 = type { i16, i16, i16 }
+// CHECK: define{{.*}} void @foo10(%struct.s10* {{.*}}, i16 %a, i16 %b, i16 %c)
+// CHECK: define{{.*}} %struct.s06 @foo06(i16 %a, i16 %b, i16 %c)
Index: clang/lib/CodeGen/TargetInfo.cpp
===================================================================
--- clang/lib/CodeGen/TargetInfo.cpp
+++ clang/lib/CodeGen/TargetInfo.cpp
@@ -8154,14 +8154,39 @@
 }
 
 //===----------------------------------------------------------------------===//
-// AVR ABI Implementation.
+// AVR ABI Implementation. Documented at
+// https://gcc.gnu.org/wiki/avr-gcc#Calling_Convention
+// https://gcc.gnu.org/wiki/avr-gcc#Reduced_Tiny
 //===----------------------------------------------------------------------===//
 
 namespace {
+class AVRABIInfo : public DefaultABIInfo {
+public:
+  AVRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) {}
+
+  ABIArgInfo classifyReturnType(QualType Ty) const {
+    // A return struct with size less than or equal to 8 bytes is returned
+    // directly via registers R18-R25.
+    if (isAggregateTypeForABI(Ty) && getContext().getTypeSize(Ty) <= 64)
+      return ABIArgInfo::getDirect();
+    else
+      return DefaultABIInfo::classifyReturnType(Ty);
+  }
+
+  // Just copy the original implementation of DefaultABIInfo::computeInfo(),
+  // since DefaultABIInfo::classify{Return,Argument}Type() are not virtual.
+  void computeInfo(CGFunctionInfo &FI) const override {
+    if (!getCXXABI().classifyReturnType(FI))
+      FI.getReturnInfo() = classifyReturnType(FI.getReturnType());
+    for (auto &I : FI.arguments())
+      I.info = classifyArgumentType(I.type);
+  }
+};
+
 class AVRTargetCodeGenInfo : public TargetCodeGenInfo {
 public:
   AVRTargetCodeGenInfo(CodeGenTypes &CGT)
-      : TargetCodeGenInfo(std::make_unique<DefaultABIInfo>(CGT)) {}
+      : TargetCodeGenInfo(std::make_unique<AVRABIInfo>(CGT)) {}
 
   LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
                                   const VarDecl *D) const override {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99237.355111.patch
Type: text/x-patch
Size: 2634 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210629/77aba7e2/attachment.bin>


More information about the cfe-commits mailing list