[flang-commits] [flang] [llvm] [LLVM] only add exclude metadata to ELF and COFF only (PR #80722)

via flang-commits flang-commits at lists.llvm.org
Mon Feb 5 10:47:35 PST 2024


https://github.com/madanial0 updated https://github.com/llvm/llvm-project/pull/80722

>From f1a9e6cd0e21fb6869c32eda1959d0fbc8c3bfda Mon Sep 17 00:00:00 2001
From: Mark Danial <madanial at dixon.rtp.raleigh.ibm.com>
Date: Mon, 5 Feb 2024 11:01:47 -0500
Subject: [PATCH] [LLVM] only add exclude metadata to ELF and COFF only

---
 flang/test/Driver/embed.f90               | 11 ++++++++---
 llvm/lib/Transforms/Utils/ModuleUtils.cpp | 10 ++++++++--
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/flang/test/Driver/embed.f90 b/flang/test/Driver/embed.f90
index 60d532a8a94fd..a47eb12df1734 100644
--- a/flang/test/Driver/embed.f90
+++ b/flang/test/Driver/embed.f90
@@ -3,12 +3,17 @@
 ! RUN lines
 !----------
 ! Embed something that can be easily checked
-! RUN: %flang_fc1 -emit-llvm -o - -fembed-offload-object=%S/Inputs/hello.f90 %s 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -emit-llvm -o - -fembed-offload-object=%S/Inputs/hello.f90 %s 2>&1 | FileCheck %s \
+! RUN:            --check-prefixes=%if system-linux || system-windows %{CHECK-EXCLUDE%} \
+! RUN:            %else %{CHECK-NOEXC%}
 
 ! RUN: %flang_fc1 -emit-llvm-bc -o %t.bc %s 2>&1
-! RUN: %flang_fc1 -emit-llvm -o - -fembed-offload-object=%S/Inputs/hello.f90 %t.bc 2>&1 | FileCheck %s
+! RUN: %flang_fc1 -emit-llvm -o - -fembed-offload-object=%S/Inputs/hello.f90 %t.bc 2>&1 | FileCheck %s \
+! RUN:            --check-prefixes=%if system-linux || system-windows %{CHECK-EXCLUDE%} \
+! RUN:            %else %{CHECK-NOEXC%}
 
-! CHECK: @[[OBJECT_1:.+]] = private constant [61 x i8] c"program hello\0A  write(*,*), \22Hello world!\22\0Aend program hello\0A", section ".llvm.offloading", align 8, !exclude !0
+! CHECK-NOEXC: @[[OBJECT_1:.+]] = private constant [61 x i8] c"program hello\0A  write(*,*), \22Hello world!\22\0Aend program hello\0A", section ".llvm.offloading", align 8
+! CHECK-EXCLUDE: @[[OBJECT_1:.+]] = private constant [61 x i8] c"program hello\0A  write(*,*), \22Hello world!\22\0Aend program hello\0A", section ".llvm.offloading", align 8, !exclude !0
 ! CHECK: @llvm.compiler.used = appending global [1 x ptr] [ptr @[[OBJECT_1]]], section "llvm.metadata"
 
 
diff --git a/llvm/lib/Transforms/Utils/ModuleUtils.cpp b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
index 209a6a34a3c9c..fd63f565e5b14 100644
--- a/llvm/lib/Transforms/Utils/ModuleUtils.cpp
+++ b/llvm/lib/Transforms/Utils/ModuleUtils.cpp
@@ -11,8 +11,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Utils/ModuleUtils.h"
-#include "llvm/Analysis/VectorUtils.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/Analysis/VectorUtils.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/IRBuilder.h"
@@ -20,6 +20,7 @@
 #include "llvm/IR/Module.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Support/xxhash.h"
+#include "llvm/TargetParser/Triple.h"
 
 using namespace llvm;
 
@@ -346,7 +347,12 @@ void llvm::embedBufferInModule(Module &M, MemoryBufferRef Buf,
                         MDString::get(Ctx, SectionName)};
 
   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
-  GV->setMetadata(LLVMContext::MD_exclude, llvm::MDNode::get(Ctx, {}));
+
+  // The exclude metadata node is only supported by ELF and COFF
+  // object file formats.
+  Triple TargetTriple = Triple(M.getTargetTriple());
+  if (TargetTriple.isOSBinFormatELF() || TargetTriple.isOSBinFormatCOFF())
+    GV->setMetadata(LLVMContext::MD_exclude, llvm::MDNode::get(Ctx, {}));
 
   appendToCompilerUsed(M, GV);
 }



More information about the flang-commits mailing list