[PATCH] D60719: Demonstrate how to fix freestanding for memcpy

Guillaume Chatelet via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 16 06:47:09 PDT 2019


gchatelet updated this revision to Diff 195369.
gchatelet added a comment.

Add test.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D60719

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGen/freestanding-disables-libc.c
  llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
  llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp


Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -5480,6 +5480,14 @@
   }
 }
 
+static bool IsForceInlineLibc(const SelectionDAG &DAG) {
+  const Module *M = DAG.getMachineFunction().getFunction().getParent();
+  if (auto *MD = mdconst::extract_or_null<ConstantInt>(
+          M->getModuleFlag("force-inline-libc")))
+    return MD->getZExtValue();
+  return false;
+}
+
 /// Lower the call to the specified intrinsic function. If we want to emit this
 /// as a call to a named external function, return the name. Otherwise, lower it
 /// and return null.
@@ -5553,10 +5561,11 @@
     unsigned Align = MinAlign(DstAlign, SrcAlign);
     bool isVol = MCI.isVolatile();
     bool isTC = I.isTailCall() && isInTailCallPosition(&I, DAG.getTarget());
+    bool IsAlwaysInline = IsForceInlineLibc(DAG);
     // FIXME: Support passing different dest/src alignments to the memcpy DAG
     // node.
     SDValue MC = DAG.getMemcpy(getRoot(), sdl, Op1, Op2, Op3, Align, isVol,
-                               false, isTC,
+                               IsAlwaysInline, isTC,
                                MachinePointerInfo(I.getArgOperand(0)),
                                MachinePointerInfo(I.getArgOperand(1)));
     updateDAGForMaybeTailCall(MC);
Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -6090,6 +6090,9 @@
   // beyond the given memory regions. But fixing this isn't easy, and most
   // people don't care.
 
+  assert(MF->getFunction().getParent()->getModuleFlag("force-inline-libc") ==
+         nullptr);
+
   // Emit a library call.
   TargetLowering::ArgListTy Args;
   TargetLowering::ArgListEntry Entry;
Index: clang/test/CodeGen/freestanding-disables-libc.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/freestanding-disables-libc.c
@@ -0,0 +1,11 @@
+// NOTE: Test that IR module specifies a "force-inline-libc" attribute in freestanding mode.
+// RUN: %clang_cc1 -triple i386-unknown-unknown -emit-llvm %s -o - | not grep 'force-inline-libc'
+// RUN: %clang_cc1 -triple i386-unknown-unknown -O2 -emit-llvm %s -o - | not grep 'force-inline-libc'
+// RUN: %clang_cc1 -triple i386-unknown-unknown -ffreestanding -O2 -emit-llvm %s -o - | grep 'force-inline-libc'
+
+// NOTE: Test that assembly doesn't call memcpy function in freestanding mode.
+// RUN: %clang_cc1 -triple i386-unknown-unknown -O2 -S %s -o - | grep 'memcpy'
+// RUN: %clang_cc1 -triple i386-unknown-unknown -ffreestanding -O2 -S %s -o - | not grep 'memcpy'
+
+struct BigStruct { char payload[4096]; };
+struct BigStruct PassByValue(struct BigStruct value) { return value; }
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -596,6 +596,10 @@
   if (!getCodeGenOpts().RecordCommandLine.empty())
     EmitCommandLineMetadata();
 
+  if (LangOpts.Freestanding) {
+    getModule().addModuleFlag(llvm::Module::Override, "force-inline-libc", 1);
+  }
+
   EmitTargetMetadata();
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60719.195369.patch
Type: text/x-patch
Size: 3439 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190416/7c029aab/attachment.bin>


More information about the cfe-commits mailing list