[PATCH] D112626: Convert float to double on __builtin_dump_struct

Rafael Franco via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 20 04:25:02 PST 2021


rafaelfranco created this revision.
rafaelfranco published this revision for review.
rafaelfranco added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This seems to fix the problem. Feedback etc. more than welcome :)


Variadic arguments of float type are automatically promoted to double.
This commit makes the __builtin_dump_struct to convert float arguments
to double before passing them to the printf-style function, fixing this
bug https://bugs.llvm.org/show_bug.cgi?id=45143.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112626

Files:
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/test/CodeGen/dump-struct-builtin.c


Index: clang/test/CodeGen/dump-struct-builtin.c
===================================================================
--- clang/test/CodeGen/dump-struct-builtin.c
+++ clang/test/CodeGen/dump-struct-builtin.c
@@ -113,6 +113,12 @@
 // CHECK-NEXT: [[FORMAT_U18:@[0-9]+]] = private unnamed_addr constant [5 x i8] c"%Lf\0A\00"
 // CHECK-NEXT: [[END_STRUCT_U18:@[0-9]+]] = private unnamed_addr constant [3 x i8] c"}\0A\00"
 
+// CHECK: @__const.unit19.a = private unnamed_addr constant %struct.U19A { float 0x3FF1F9AD00000000 }, align 4
+// CHECK-NEXT: [[STRUCT_STR_U19:@[0-9]+]] = private unnamed_addr constant [15 x i8] c"struct U19A {\0A\00"
+// CHECK-NEXT: [[FIELD_U19:@[0-9]+]] = private unnamed_addr constant [11 x i8] c"float a : \00"
+// CHECK-NEXT: [[FORMAT_U19:@[0-9]+]] = private unnamed_addr constant [4 x i8] c"%f\0A\00"
+// CHECK-NEXT: [[END_STRUCT_U19:@[0-9]+]] = private unnamed_addr constant [3 x i8] c"}\0A\00"
+
 int printf(const char *fmt, ...) {
     return 0;
 }
@@ -553,3 +559,22 @@
   // CHECK: call i32 (i8*, ...) @printf(
   __builtin_dump_struct(&a, &printf);
 }
+
+void unit19() {
+  struct U19A {
+    float a;
+  };
+
+  struct U19A a = {
+      .a = 1.123456f,
+  };
+
+  // CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* [[STRUCT_STR_U19]], i32 0, i32 0))
+  // CHECK: [[RES1:%[0-9]+]] = getelementptr inbounds %struct.U19A, %struct.U19A* %a, i32 0, i32 0
+  // CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* [[FIELD_U19]], i32 0, i32 0))
+  // CHECK: [[LOAD1:%[0-9]+]] = load float, float* [[RES1]],
+  // CHECK: [[FPEXT1:%[0-9]+]] = fpext float [[LOAD1]] to double
+  // CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* [[FORMAT_U19]], i32 0, i32 0), double [[FPEXT1]])
+  // CHECK: call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* [[END_STRUCT_U19]], i32 0, i32 0)
+  __builtin_dump_struct(&a, &printf);
+}
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -2087,6 +2087,12 @@
     Address FieldAddress = Address(FieldPtr, Align);
     FieldPtr = CGF.Builder.CreateLoad(FieldAddress);
 
+    // Variadic functions expect the caller to promote float to double.
+    if (CanonicalType == Context.FloatTy) {
+      FieldPtr =
+          CGF.Builder.CreateFPExt(FieldPtr, CGF.ConvertType(Context.DoubleTy));
+    }
+
     // FIXME Need to handle bitfield here
     GString = CGF.Builder.CreateGlobalStringPtr(
         Format.concat(llvm::Twine('\n')).str());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112626.382666.patch
Type: text/x-patch
Size: 2652 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211120/f3bdf9ab/attachment-0001.bin>


More information about the cfe-commits mailing list