[llvm-branch-commits] [llvm] 5adea1c - [RISCV] Use OS-specific stack-guard ABI for Fuchsia

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Roland McGrath
Date: 2023-02-16T09:48:40+01:00
New Revision: 5adea1c777b4eff79f9b6308aefa513c8ccc78cf

URL: 
https://github.com/llvm/llvm-project/commit/5adea1c777b4eff79f9b6308aefa513c8ccc78cf
DIFF: 
https://github.com/llvm/llvm-project/commit/5adea1c777b4eff79f9b6308aefa513c8ccc78cf.diff

LOG: [RISCV] Use OS-specific stack-guard ABI for Fuchsia

Fuchsia provides a slot relative to tp for the stack-guard value,
which is cheaper to materialize than the default GOT load.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D143353

Added: 
llvm/test/CodeGen/RISCV/stack-protector-target.ll

Modified: 
llvm/lib/Target/RISCV/RISCVISelLowering.cpp
llvm/lib/Target/RISCV/RISCVISelLowering.h
llvm/lib/Target/RISCV/RISCVSubtarget.h

Removed: 




diff  --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp 
b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index a8720d070acba..6eea169f89199 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -14211,6 +14211,25 @@ bool 
RISCVTargetLowering::preferScalarizeSplat(unsigned Opc) const {
   return true;
 }
 
+static Value *useTpOffset(IRBuilderBase &IRB, unsigned Offset) {
+  Module *M = IRB.GetInsertBlock()->getParent()->getParent();
+  Function *ThreadPointerFunc =
+  Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
+  return IRB.CreatePointerCast(
+  IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
+ IRB.CreateCall(ThreadPointerFunc), Offset),
+  IRB.getInt8PtrTy()->getPointerTo(0));
+}
+
+Value *RISCVTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
+  // Fuchsia provides a fixed TLS slot for the stack cookie.
+  //  defines ZX_TLS_STACK_GUARD_OFFSET with this value.
+  if (Subtarget.isTargetFuchsia())
+return useTpOffset(IRB, -0x10);
+
+  return TargetLowering::getIRStackGuard(IRB);
+}
+
 #define GET_REGISTER_MATCHER
 #include "RISCVGenAsmMatcher.inc"
 

diff  --git a/llvm/lib/Target/RISCV/RISCVISelLowering.h 
b/llvm/lib/Target/RISCV/RISCVISelLowering.h
index acf92cab35984..5086e6c0bdafb 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.h
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.h
@@ -618,6 +618,10 @@ class RISCVTargetLowering : public TargetLowering {
 return Scale == 1;
   }
 
+  /// If the target has a standard location for the stack protector cookie,
+  /// returns the address of that location. Otherwise, returns nullptr.
+  Value *getIRStackGuard(IRBuilderBase &IRB) const override;
+
 private:
   /// RISCVCCAssignFn - This target-specific function extends the default
   /// CCValAssign with additional information used to lower RISC-V calling

diff  --git a/llvm/lib/Target/RISCV/RISCVSubtarget.h 
b/llvm/lib/Target/RISCV/RISCVSubtarget.h
index 678d218a6719c..290c7b03ea819 100644
--- a/llvm/lib/Target/RISCV/RISCVSubtarget.h
+++ b/llvm/lib/Target/RISCV/RISCVSubtarget.h
@@ -176,6 +176,8 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
   const LegalizerInfo *getLegalizerInfo() const override;
   const RegisterBankInfo *getRegBankInfo() const override;
 
+  bool isTargetFuchsia() const { return getTargetTriple().isOSFuchsia(); }
+
   bool useConstantPoolForLargeInts() const;
 
   // Maximum cost used for building integers, integers will be put into 
constant

diff  --git a/llvm/test/CodeGen/RISCV/stack-protector-target.ll 
b/llvm/test/CodeGen/RISCV/stack-protector-target.ll
new file mode 100644
index 0..cd5afe3467afc
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/stack-protector-target.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+
+;; Test target-specific stack cookie location.
+;
+; RUN: llc -mtriple=riscv64-fuchsia < %s | FileCheck 
--check-prefix=FUCHSIA-RISCV64 %s
+
+define void @func() sspreq {
+; FUCHSIA-RISCV64-LABEL: func:
+; FUCHSIA-RISCV64:   # %bb.0:
+; FUCHSIA-RISCV64-NEXT:addi sp, sp, -32
+; FUCHSIA-RISCV64-NEXT:.cfi_def_cfa_offset 32
+; FUCHSIA-RISCV64-NEXT:sd ra, 24(sp) # 8-byte Folded Spill
+; FUCHSIA-RISCV64-NEXT:.cfi_offset ra, -8
+; FUCHSIA-RISCV64-NEXT:ld a0, -16(tp)
+; FUCHSIA-RISCV64-NEXT:sd a0, 16(sp)
+; FUCHSIA-RISCV64-NEXT:addi a0, sp, 12
+; FUCHSIA-RISCV64-NEXT:call capture@plt
+; FUCHSIA-RISCV64-NEXT:ld a0, -16(tp)
+; FUCHSIA-RISCV64-NEXT:ld a1, 16(sp)
+; FUCHSIA-RISCV64-NEXT:bne a0, a1, .LBB0_2
+; FUCHSIA-RISCV64-NEXT:  # %bb.1: # %SP_return
+; FUCHSIA-RISCV64-NEXT:ld ra, 24(sp) # 8-byte Folded Reload
+; FUCHSIA-RISCV64-NEXT:addi sp, sp, 32
+; FUCHSIA-RISCV64-NEXT:ret
+; FUCHSIA-RISCV64-NEXT:  .LBB0_2: # %CallStackCheckFailBlk
+; FUCHSIA-RISCV64-NEXT:call __stack_chk_fail@plt
+  %1 = alloca i32, align 4
+  call void @capture(ptr nonnull %1)
+  ret void
+}
+
+declare void @capture(ptr)



___
llvm-branch-commits mailing

[llvm-branch-commits] [clang] 9a8f294 - [RISCV] Default to -fsanitize=shadow-call-stack for Fuchsia

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Roland McGrath
Date: 2023-02-16T09:48:40+01:00
New Revision: 9a8f294b11019387270981034b279d182b263e5c

URL: 
https://github.com/llvm/llvm-project/commit/9a8f294b11019387270981034b279d182b263e5c
DIFF: 
https://github.com/llvm/llvm-project/commit/9a8f294b11019387270981034b279d182b263e5c.diff

LOG: [RISCV] Default to -fsanitize=shadow-call-stack for Fuchsia

The ShadowCallStack is the preferred and default ABI for Fuchsia.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D143357

Added: 


Modified: 
clang/lib/Driver/ToolChains/Fuchsia.cpp
clang/test/Driver/fuchsia.c

Removed: 




diff  --git a/clang/lib/Driver/ToolChains/Fuchsia.cpp 
b/clang/lib/Driver/ToolChains/Fuchsia.cpp
index 9bdc3a7917791..ea6d7d697770b 100644
--- a/clang/lib/Driver/ToolChains/Fuchsia.cpp
+++ b/clang/lib/Driver/ToolChains/Fuchsia.cpp
@@ -447,13 +447,13 @@ SanitizerMask Fuchsia::getDefaultSanitizers() const {
   SanitizerMask Res;
   switch (getTriple().getArch()) {
   case llvm::Triple::aarch64:
+  case llvm::Triple::riscv64:
 Res |= SanitizerKind::ShadowCallStack;
 break;
   case llvm::Triple::x86_64:
 Res |= SanitizerKind::SafeStack;
 break;
   default:
-// TODO: Enable SafeStack on RISC-V once tested.
 break;
   }
   return Res;

diff  --git a/clang/test/Driver/fuchsia.c b/clang/test/Driver/fuchsia.c
index 25a2f841a9cb0..d16e792b2c60f 100644
--- a/clang/test/Driver/fuchsia.c
+++ b/clang/test/Driver/fuchsia.c
@@ -31,6 +31,7 @@
 // CHECK: "-isysroot" "[[SYSROOT:[^"]+]]"
 // CHECK: "-internal-externc-isystem" "[[SYSROOT]]{{/|}}include"
 // CHECK-AARCH64: "-fsanitize=shadow-call-stack"
+// CHECK-RISCV64: "-fsanitize=shadow-call-stack"
 // CHECK-X86_64: "-fsanitize=safe-stack"
 // CHECK: "-stack-protector" "2"
 // CHECK-AARCH64: "-target-feature" "+outline-atomics"



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] b6089c6 - [RISCV] Default to -ffixed-x18 for Fuchsia

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Roland McGrath
Date: 2023-02-16T09:48:40+01:00
New Revision: b6089c624595c781766407b6ca5c2cc250ceff8b

URL: 
https://github.com/llvm/llvm-project/commit/b6089c624595c781766407b6ca5c2cc250ceff8b
DIFF: 
https://github.com/llvm/llvm-project/commit/b6089c624595c781766407b6ca5c2cc250ceff8b.diff

LOG: [RISCV] Default to -ffixed-x18 for Fuchsia

Fuchsia's ABI always reserves the x18 (s2) register for the
ShadowCallStack ABI, even when -fsanitize=shadow-call-stack is
not enabled.

Reviewed By: phosek

Differential Revision: https://reviews.llvm.org/D143355

Added: 


Modified: 
clang/lib/Driver/SanitizerArgs.cpp
clang/test/Driver/sanitizer-ld.c
llvm/include/llvm/TargetParser/RISCVTargetParser.h
llvm/lib/Target/RISCV/RISCVSubtarget.cpp
llvm/lib/TargetParser/RISCVTargetParser.cpp
llvm/test/CodeGen/RISCV/reserved-regs.ll

Removed: 




diff  --git a/clang/lib/Driver/SanitizerArgs.cpp 
b/clang/lib/Driver/SanitizerArgs.cpp
index 52bee6a755ff4..068a34a54a92c 100644
--- a/clang/lib/Driver/SanitizerArgs.cpp
+++ b/clang/lib/Driver/SanitizerArgs.cpp
@@ -19,6 +19,7 @@
 #include "llvm/Support/SpecialCaseList.h"
 #include "llvm/Support/TargetParser.h"
 #include "llvm/Support/VirtualFileSystem.h"
+#include "llvm/TargetParser/RISCVTargetParser.h"
 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
 #include 
 
@@ -545,7 +546,8 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
   if ((Kinds & SanitizerKind::ShadowCallStack) &&
   ((TC.getTriple().isAArch64() &&
 !llvm::AArch64::isX18ReservedByDefault(TC.getTriple())) ||
-   TC.getTriple().isRISCV()) &&
+   (TC.getTriple().isRISCV() &&
+!llvm::RISCV::isX18ReservedByDefault(TC.getTriple( &&
   !Args.hasArg(options::OPT_ffixed_x18) && DiagnoseErrors) {
 D.Diag(diag::err_drv_argument_only_allowed_with)
 << lastArgumentForMask(D, Args, Kinds & SanitizerKind::ShadowCallStack)

diff  --git a/clang/test/Driver/sanitizer-ld.c 
b/clang/test/Driver/sanitizer-ld.c
index 6b85abfd81c55..b55f6d67bc6d2 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -699,6 +699,11 @@
 // RUN:   | FileCheck --check-prefix=CHECK-SHADOWCALLSTACK-LINUX-RISCV64 %s
 // CHECK-SHADOWCALLSTACK-LINUX-RISCV64: '-fsanitize=shadow-call-stack' only 
allowed with '-ffixed-x18'
 
+// RUN: %clang -fsanitize=shadow-call-stack -### %s 2>&1 \
+// RUN: --target=riscv64-unknown-fuchsia -fuse-ld=ld \
+// RUN:   | FileCheck --check-prefix=CHECK-SHADOWCALLSTACK-FUCHSIA-RISCV64 %s
+// CHECK-SHADOWCALLSTACK-FUCHSIA-RISCV64-NOT: error:
+
 // RUN: %clang -fsanitize=shadow-call-stack -### %s 2>&1 \
 // RUN: --target=aarch64-unknown-linux -fuse-ld=ld -ffixed-x18 \
 // RUN:   | FileCheck --check-prefix=CHECK-SHADOWCALLSTACK-LINUX-AARCH64-X18 %s

diff  --git a/llvm/include/llvm/TargetParser/RISCVTargetParser.h 
b/llvm/include/llvm/TargetParser/RISCVTargetParser.h
index da2ecd8c1339d..f50576b8fee16 100644
--- a/llvm/include/llvm/TargetParser/RISCVTargetParser.h
+++ b/llvm/include/llvm/TargetParser/RISCVTargetParser.h
@@ -18,6 +18,9 @@
 #include 
 
 namespace llvm {
+
+class Triple;
+
 namespace RISCV {
 
 // We use 64 bits as the known part in the scalable vector types.
@@ -38,6 +41,8 @@ void fillValidCPUArchList(SmallVectorImpl &Values, 
bool IsRV64);
 void fillValidTuneCPUArchList(SmallVectorImpl &Values, bool IsRV64);
 bool getCPUFeaturesExceptStdExt(CPUKind Kind, std::vector 
&Features);
 
+bool isX18ReservedByDefault(const Triple &TT);
+
 } // namespace RISCV
 } // namespace llvm
 

diff  --git a/llvm/lib/Target/RISCV/RISCVSubtarget.cpp 
b/llvm/lib/Target/RISCV/RISCVSubtarget.cpp
index c935dad1687f4..d0bb4634d03d4 100644
--- a/llvm/lib/Target/RISCV/RISCVSubtarget.cpp
+++ b/llvm/lib/Target/RISCV/RISCVSubtarget.cpp
@@ -83,6 +83,9 @@ RISCVSubtarget::RISCVSubtarget(const Triple &TT, StringRef 
CPU,
   FrameLowering(
   initializeSubtargetDependencies(TT, CPU, TuneCPU, FS, ABIName)),
   InstrInfo(*this), RegInfo(getHwMode()), TLInfo(TM, *this) {
+  if (RISCV::isX18ReservedByDefault(TT))
+UserReservedRegister.set(RISCV::X18);
+
   CallLoweringInfo.reset(new RISCVCallLowering(*getTargetLowering()));
   Legalizer.reset(new RISCVLegalizerInfo(*this));
 

diff  --git a/llvm/lib/TargetParser/RISCVTargetParser.cpp 
b/llvm/lib/TargetParser/RISCVTargetParser.cpp
index 89cd5c082d72d..933a82b7c6cb2 100644
--- a/llvm/lib/TargetParser/RISCVTargetParser.cpp
+++ b/llvm/lib/TargetParser/RISCVTargetParser.cpp
@@ -14,6 +14,7 @@
 #include "llvm/TargetParser/RISCVTargetParser.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringSwitch.h"
+#include "llvm/TargetParser/Triple.h"
 
 namespace llvm {
 namespace RISCV {
@@ -100,5 +101,10 @@ bool getCPUFeaturesExceptStdExt(CPUKind Kind,
   return true;
 }
 
+bool isX18ReservedByDefault(const Triple &TT) {
+  // X18 is reserved for the ShadowCallStack ABI (ev

[llvm-branch-commits] [llvm] 7f25508 - [RISCV] Clean up stack-protector-target test

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Roland McGrath
Date: 2023-02-16T09:48:40+01:00
New Revision: 7f255080a6cde7fd1cb30b67b9ffb8255c32a4b0

URL: 
https://github.com/llvm/llvm-project/commit/7f255080a6cde7fd1cb30b67b9ffb8255c32a4b0
DIFF: 
https://github.com/llvm/llvm-project/commit/7f255080a6cde7fd1cb30b67b9ffb8255c32a4b0.diff

LOG: [RISCV] Clean up stack-protector-target test

Add some missed polish from https://reviews.llvm.org/D143355.

Reviewed By: jrtc27

Differential Revision: https://reviews.llvm.org/D143360

Added: 


Modified: 
llvm/test/CodeGen/RISCV/stack-protector-target.ll

Removed: 




diff  --git a/llvm/test/CodeGen/RISCV/stack-protector-target.ll 
b/llvm/test/CodeGen/RISCV/stack-protector-target.ll
index cd5afe3467af..410b89df1f35 100644
--- a/llvm/test/CodeGen/RISCV/stack-protector-target.ll
+++ b/llvm/test/CodeGen/RISCV/stack-protector-target.ll
@@ -4,13 +4,11 @@
 ;
 ; RUN: llc -mtriple=riscv64-fuchsia < %s | FileCheck 
--check-prefix=FUCHSIA-RISCV64 %s
 
-define void @func() sspreq {
+define void @func() sspreq nounwind {
 ; FUCHSIA-RISCV64-LABEL: func:
 ; FUCHSIA-RISCV64:   # %bb.0:
 ; FUCHSIA-RISCV64-NEXT:addi sp, sp, -32
-; FUCHSIA-RISCV64-NEXT:.cfi_def_cfa_offset 32
 ; FUCHSIA-RISCV64-NEXT:sd ra, 24(sp) # 8-byte Folded Spill
-; FUCHSIA-RISCV64-NEXT:.cfi_offset ra, -8
 ; FUCHSIA-RISCV64-NEXT:ld a0, -16(tp)
 ; FUCHSIA-RISCV64-NEXT:sd a0, 16(sp)
 ; FUCHSIA-RISCV64-NEXT:addi a0, sp, 12
@@ -25,7 +23,7 @@ define void @func() sspreq {
 ; FUCHSIA-RISCV64-NEXT:  .LBB0_2: # %CallStackCheckFailBlk
 ; FUCHSIA-RISCV64-NEXT:call __stack_chk_fail@plt
   %1 = alloca i32, align 4
-  call void @capture(ptr nonnull %1)
+  call void @capture(ptr %1)
   ret void
 }
 



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] d0277dc - [hexagon] Turning off sign mismatch warning by default.

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Brian Cain
Date: 2023-02-16T09:48:31+01:00
New Revision: d0277dcd5fde9b7a5f6247ae8d1cedd189c0e32b

URL: 
https://github.com/llvm/llvm-project/commit/d0277dcd5fde9b7a5f6247ae8d1cedd189c0e32b
DIFF: 
https://github.com/llvm/llvm-project/commit/d0277dcd5fde9b7a5f6247ae8d1cedd189c0e32b.diff

LOG: [hexagon] Turning off sign mismatch warning by default.

Patch-by: Colin Lemahieu 

Differential Revision: https://reviews.llvm.org/D143531

(cherry picked from commit f1a87d476cc1d4a361917ed7e44e11ac5bc5636d)

Added: 


Modified: 
llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp

Removed: 




diff  --git a/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp 
b/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
index 248bd3130c250..9a2cf94edfe66 100644
--- a/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
+++ b/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
@@ -71,7 +71,7 @@ static cl::opt ErrorMissingParenthesis(
 static cl::opt WarnSignedMismatch(
 "mwarn-sign-mismatch",
 cl::desc("Warn for mismatching a signed and unsigned value"),
-cl::init(true));
+cl::init(false));
 static cl::opt WarnNoncontigiousRegister(
 "mwarn-noncontigious-register",
 cl::desc("Warn for register names that arent contigious"), cl::init(true));



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] 0f02cdb - [hexagon] add a -mcabac flag

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Brian Cain
Date: 2023-02-16T09:48:46+01:00
New Revision: 0f02cdb9dca6ead87820a430e28adaa3cdb624ca

URL: 
https://github.com/llvm/llvm-project/commit/0f02cdb9dca6ead87820a430e28adaa3cdb624ca
DIFF: 
https://github.com/llvm/llvm-project/commit/0f02cdb9dca6ead87820a430e28adaa3cdb624ca.diff

LOG: [hexagon] add a -mcabac flag

For v73 and later, clang users who wish to use the cabac instructions need
a way to add the 'cabac' target feature.

Reviewed By: kparzysz

Differential Revision: https://reviews.llvm.org/D142947

(cherry picked from commit cd97675e02368f31d56bd8f143c5987649d162d8)

Added: 


Modified: 
clang/include/clang/Driver/Options.td
clang/test/Driver/hexagon-toolchain-elf.c

Removed: 




diff  --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 23752823e88f6..652c15afcce86 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4559,6 +4559,8 @@ def mnvs : Flag<["-"], "mnvs">, 
Group,
   Flags<[CC1Option]>, HelpText<"Enable generation of new-value stores">;
 def mno_nvs : Flag<["-"], "mno-nvs">, Group,
   Flags<[CC1Option]>, HelpText<"Disable generation of new-value stores">;
+def mcabac: Flag<["-"], "mcabac">, Group,
+  HelpText<"Enable CABAC instructions">;
 
 // SPARC feature flags
 def mfpu : Flag<["-"], "mfpu">, Group;

diff  --git a/clang/test/Driver/hexagon-toolchain-elf.c 
b/clang/test/Driver/hexagon-toolchain-elf.c
index 477bc8a969b92..9454c33747630 100644
--- a/clang/test/Driver/hexagon-toolchain-elf.c
+++ b/clang/test/Driver/hexagon-toolchain-elf.c
@@ -581,6 +581,16 @@
 // RUN:| FileCheck --check-prefix=CHECK371 %s
 // CHECK371-NOT: "+reserved-r19"
 
+// 
-
+// mcabac
+// 
-
+// RUN: %clang -### -target hexagon-unknown-elf -mcabac %s 2>&1 \
+// RUN:| FileCheck --check-prefix=CHECK372 %s
+// CHECK372: "-target-feature" "+cabac"
+// RUN: %clang -### -target hexagon-unknown-elf %s 2>&1 \
+// RUN:| FileCheck --check-prefix=CHECK373 %s
+// CHECK373-NOT: "+cabac"
+
 // 
-
 // Misc Defaults
 // 
-



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 3aa210b - [Symbolize][MinGW] Support demangling i386 call-conv-decorated C++ names

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Alvin Wong
Date: 2023-02-16T09:48:54+01:00
New Revision: 3aa210b13a229528a8ef1a5a48ada0e57f5704b0

URL: 
https://github.com/llvm/llvm-project/commit/3aa210b13a229528a8ef1a5a48ada0e57f5704b0
DIFF: 
https://github.com/llvm/llvm-project/commit/3aa210b13a229528a8ef1a5a48ada0e57f5704b0.diff

LOG: [Symbolize][MinGW] Support demangling i386 call-conv-decorated C++ names

On i386 Windows, after C++ names have been Itanium-mangled, the C name
mangling specific to its call convention may also be applied on top.
This change teaches symbolizer to be able to demangle this type of
mangled names.

As part of this change, `demanglePE32ExternCFunc` has also been modified
to fix unwanted stripping for vectorcall names when the demangled name
is supposed to contain a leading `_`. Notice that the vectorcall
mangling does not add either an `_` or `@` prefix. The old code always
tries to strip the prefix first, which for Itanium mangled names in
vectorcall, the leading underscore of the Itanium name gets stripped
instead and breaks the Itanium demangler.

Differential Revision: https://reviews.llvm.org/D144049

(cherry picked from commit e117fd28d525ead14b809da3a9b5ef22710ca351)

Added: 
llvm/test/DebugInfo/symbolize-demangling-mingw32.s

Modified: 
llvm/lib/DebugInfo/Symbolize/Symbolize.cpp

Removed: 




diff  --git a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp 
b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp
index 72c008d9835ec..a92dfbcc5c64f 100644
--- a/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp
+++ b/llvm/lib/DebugInfo/Symbolize/Symbolize.cpp
@@ -649,22 +649,29 @@ namespace {
 // vectorcall  - foo@@12
 // These are all 
diff erent linkage names for 'foo'.
 StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
-  // Remove any '_' or '@' prefix.
   char Front = SymbolName.empty() ? '\0' : SymbolName[0];
-  if (Front == '_' || Front == '@')
-SymbolName = SymbolName.drop_front();
 
   // Remove any '@[0-9]+' suffix.
+  bool HasAtNumSuffix = false;
   if (Front != '?') {
 size_t AtPos = SymbolName.rfind('@');
 if (AtPos != StringRef::npos &&
-all_of(drop_begin(SymbolName, AtPos + 1), isDigit))
+all_of(drop_begin(SymbolName, AtPos + 1), isDigit)) {
   SymbolName = SymbolName.substr(0, AtPos);
+  HasAtNumSuffix = true;
+}
   }
 
   // Remove any ending '@' for vectorcall.
-  if (SymbolName.endswith("@"))
+  bool IsVectorCall = false;
+  if (HasAtNumSuffix && SymbolName.endswith("@")) {
 SymbolName = SymbolName.drop_back();
+IsVectorCall = true;
+  }
+
+  // If not vectorcall, remove any '_' or '@' prefix.
+  if (!IsVectorCall && (Front == '_' || Front == '@'))
+SymbolName = SymbolName.drop_front();
 
   return SymbolName;
 }
@@ -692,8 +699,14 @@ LLVMSymbolizer::DemangleName(const std::string &Name,
 return Result;
   }
 
-  if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module())
-return std::string(demanglePE32ExternCFunc(Name));
+  if (DbiModuleDescriptor && DbiModuleDescriptor->isWin32Module()) {
+std::string DemangledCName(demanglePE32ExternCFunc(Name));
+// On i386 Windows, the C name mangling for 
diff erent calling conventions
+// may also be applied on top of the Itanium or Rust name mangling.
+if (nonMicrosoftDemangle(DemangledCName.c_str(), Result))
+  return Result;
+return DemangledCName;
+  }
   return Name;
 }
 

diff  --git a/llvm/test/DebugInfo/symbolize-demangling-mingw32.s 
b/llvm/test/DebugInfo/symbolize-demangling-mingw32.s
new file mode 100644
index 0..1b445db92e6b0
--- /dev/null
+++ b/llvm/test/DebugInfo/symbolize-demangling-mingw32.s
@@ -0,0 +1,124 @@
+# REQUIRES: x86-registered-target
+
+# RUN: llvm-mc --filetype=obj --triple=i386-w64-windows-gnu %s -o %t.o -g
+
+# RUN: llvm-symbolizer --obj=%t.o 0 1 2 3 4 5 6 7 8 9 10 | FileCheck %s
+
+  .def g
+  .scl 2
+  .type 32
+  .endef
+g:
+  nop
+# CHECK:   {{^g$}}
+# CHECK-NEXT:  symbolize-demangling-mingw32.s:12
+# CHECK-EMPTY:
+
+  .def baz
+  .scl 2
+  .type 32
+  .endef
+baz:
+  nop
+# CHECK-NEXT:  {{^baz$}}
+# CHECK-NEXT:  symbolize-demangling-mingw32.s:22
+# CHECK-EMPTY:
+
+# extern "C" void c() {} // __cdecl
+  .def _c
+  .scl 2
+  .type 32
+  .endef
+_c:
+  nop
+# CHECK-NEXT:  {{^c$}}
+# CHECK-NEXT:  symbolize-demangling-mingw32.s:33
+# CHECK-EMPTY:
+
+# extern "C" void __stdcall c1() {}
+  .def _c1@0
+  .scl 2
+  .type 32
+  .endef
+_c1@0:
+  nop
+# CHECK-NEXT:  {{^c1$}}
+# CHECK-NEXT:  symbolize-demangling-mingw32.s:44
+# CHECK-EMPTY:
+
+# extern "C" void __fastcall c2(void) {}
+  .def @c2@0
+  .scl 2
+  .type 32
+  .endef
+@c2@0:
+  nop
+# CHECK-NEXT:  {{^c2$}}
+# CHECK-NEXT:  symbolize-demangling-mingw32.s:55
+# CHECK-EMPTY:
+
+# extern "C" void __vectorcall c3(void) {}
+  .def c3@@0
+  .scl 2
+  .type 32
+  .endef
+c3@@0:
+  nop
+# CHECK-NEXT:  {{^c3$}}
+# CHECK-NEXT:  symbolize-demangling-mingw32.s:66
+# CHECK-EMPTY:
+
+# void f() {} // __cde

[llvm-branch-commits] [llvm] 6876abd - [Support/BLAKE3] Rename blake3_* -> llvm_blake3_* to avoid symbol collisions

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Jeremy Maitin-Shepard
Date: 2023-02-16T09:49:02+01:00
New Revision: 6876abdeaee7e0c59f9088e42263c48c977dca36

URL: 
https://github.com/llvm/llvm-project/commit/6876abdeaee7e0c59f9088e42263c48c977dca36
DIFF: 
https://github.com/llvm/llvm-project/commit/6876abdeaee7e0c59f9088e42263c48c977dca36.diff

LOG: [Support/BLAKE3] Rename blake3_* -> llvm_blake3_* to avoid symbol 
collisions

While some blake3 symbols are already prefixed, a number of symbols with hidden 
visibility have been left without an `llvm_` prefix.  This results in symbol 
collisions when statically linking llvm into a binary that also uses the 
external blake3 library.

Reviewed By: akyrtzi, MaskRay

Differential Revision: https://reviews.llvm.org/D143981

(cherry picked from commit 26662ac010ef50e65e2774eab84f325aa09360fe)

Added: 
llvm/lib/Support/BLAKE3/llvm_blake3_prefix.h

Modified: 
llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S
llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_gnu.S
llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_msvc.asm
llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S
llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_gnu.S
llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_msvc.asm
llvm/lib/Support/BLAKE3/blake3_impl.h
llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_unix.S
llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_windows_gnu.S
llvm/lib/Support/BLAKE3/blake3_sse2_x86-64_windows_msvc.asm
llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_unix.S
llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_windows_gnu.S
llvm/lib/Support/BLAKE3/blake3_sse41_x86-64_windows_msvc.asm

Removed: 




diff  --git a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S 
b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S
index 449e074928324..69fc0936d73c5 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S
+++ b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_unix.S
@@ -1,5 +1,7 @@
 #if defined(__x86_64__)
 
+#include "llvm_blake3_prefix.h"
+
 #if defined(__ELF__) && defined(__linux__)
 .section .note.GNU-stack,"",%progbits
 #endif

diff  --git a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_gnu.S 
b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_gnu.S
index bb58d2ae64b13..5ad1c641a7fc3 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_gnu.S
+++ b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_gnu.S
@@ -1,3 +1,5 @@
+#include "llvm_blake3_prefix.h"
+
 .intel_syntax noprefix
 .global _blake3_hash_many_avx2
 .global blake3_hash_many_avx2

diff  --git a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_msvc.asm 
b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_msvc.asm
index 352298edd2e8a..46bad1d98f38c 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_msvc.asm
+++ b/llvm/lib/Support/BLAKE3/blake3_avx2_x86-64_windows_msvc.asm
@@ -1,11 +1,11 @@
-public _blake3_hash_many_avx2
-public blake3_hash_many_avx2
+public _llvm_blake3_hash_many_avx2
+public llvm_blake3_hash_many_avx2
 
 _TEXT   SEGMENT ALIGN(16) 'CODE'
 
 ALIGN   16
-blake3_hash_many_avx2 PROC
-_blake3_hash_many_avx2 PROC
+llvm_blake3_hash_many_avx2 PROC
+_llvm_blake3_hash_many_avx2 PROC
 pushr15
 pushr14
 pushr13
@@ -1785,8 +1785,8 @@ endroundloop1:
 vmovdqu xmmword ptr [rbx+10H], xmm1
 jmp unwind
 
-_blake3_hash_many_avx2 ENDP
-blake3_hash_many_avx2 ENDP
+_llvm_blake3_hash_many_avx2 ENDP
+llvm_blake3_hash_many_avx2 ENDP
 _TEXT ENDS
 
 _RDATA SEGMENT READONLY PAGE ALIAS(".rdata") 'CONST'

diff  --git a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S 
b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S
index 3afc0e2250e2d..f04a135dd1bc4 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S
+++ b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_unix.S
@@ -1,5 +1,7 @@
 #if defined(__x86_64__)
 
+#include "llvm_blake3_prefix.h"
+
 #if defined(__ELF__) && defined(__linux__)
 .section .note.GNU-stack,"",%progbits
 #endif

diff  --git a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_gnu.S 
b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_gnu.S
index e10b9f36cbccb..53c586141fbef 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_gnu.S
+++ b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_gnu.S
@@ -1,3 +1,5 @@
+#include "llvm_blake3_prefix.h"
+
 .intel_syntax noprefix
 
 .global _blake3_hash_many_avx512

diff  --git a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_msvc.asm 
b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_msvc.asm
index b19efbaaeb362..f13d1b260ab8a 100644
--- a/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_msvc.asm
+++ b/llvm/lib/Support/BLAKE3/blake3_avx512_x86-64_windows_msvc.asm
@@ -1,15 +1,15 @@
-public _blake3_hash_many_avx512
-public blake3_hash_many_avx512
-public blake3_compress_in_place_avx512
-public _blake3_compress_in_place_avx512
-public blake3_compress_xo

[llvm-branch-commits] [openmp] fe8b4ac - [OpenMP] Recover non-blocking target nowait disabled by D141232

2023-02-16 Thread Tobias Hieta via llvm-branch-commits

Author: Ye Luo
Date: 2023-02-16T09:49:09+01:00
New Revision: fe8b4acbb578523a3a3fa5bf324b9ab9274c6f47

URL: 
https://github.com/llvm/llvm-project/commit/fe8b4acbb578523a3a3fa5bf324b9ab9274c6f47
DIFF: 
https://github.com/llvm/llvm-project/commit/fe8b4acbb578523a3a3fa5bf324b9ab9274c6f47.diff

LOG: [OpenMP] Recover non-blocking target nowait disabled by D141232

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D143871

(cherry picked from commit 0d4e55ba69c1dd04913c59d39c764c8070e55c60)

Added: 


Modified: 
openmp/libomptarget/src/interface.cpp

Removed: 




diff  --git a/openmp/libomptarget/src/interface.cpp 
b/openmp/libomptarget/src/interface.cpp
index 91dda62b0c2d5..beea0c26210bc 100644
--- a/openmp/libomptarget/src/interface.cpp
+++ b/openmp/libomptarget/src/interface.cpp
@@ -299,8 +299,12 @@ EXTERN int __tgt_target_kernel(ident_t *Loc, int64_t 
DeviceId, int32_t NumTeams,
int32_t ThreadLimit, void *HostPtr,
KernelArgsTy *KernelArgs) {
   TIMESCOPE_WITH_IDENT(Loc);
-  return targetKernel(Loc, DeviceId, NumTeams, ThreadLimit,
-   HostPtr, KernelArgs);
+  if (KernelArgs->Flags.NoWait)
+return targetKernel(
+Loc, DeviceId, NumTeams, ThreadLimit, HostPtr, KernelArgs);
+  else
+return targetKernel(Loc, DeviceId, NumTeams, ThreadLimit,
+ HostPtr, KernelArgs);
 }
 
 /// Implements a target kernel entry that replays a pre-recorded kernel.



___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [lld] e6c034b - [TextAPI] Implement TBDv5 Reader

2023-02-16 Thread Cyndy Ishida via llvm-branch-commits

Author: Cyndy Ishida
Date: 2023-02-16T13:51:08-08:00
New Revision: e6c034b0e43a062274e5bfefb47bd95201ec5899

URL: 
https://github.com/llvm/llvm-project/commit/e6c034b0e43a062274e5bfefb47bd95201ec5899
DIFF: 
https://github.com/llvm/llvm-project/commit/e6c034b0e43a062274e5bfefb47bd95201ec5899.diff

LOG: [TextAPI] Implement TBDv5 Reader

Introduce initial reader for TBDv5 which is in JSON. This captures all
the currently understood fields within the internal structure
`InterfaceFile`.

New fields & follow up tests will be followed up in future PRs.

Differential Revision: https://reviews.llvm.org/D144156

Added: 
llvm/lib/TextAPI/TextStubV5.cpp
llvm/unittests/TextAPI/TextStubV5Tests.cpp

Modified: 
lld/test/MachO/Inputs/libStubLink.tbd
lld/test/MachO/invalid/invalid-stub.s
lld/test/MachO/tapi-link.s
llvm/include/llvm/TextAPI/InterfaceFile.h
llvm/lib/TextAPI/CMakeLists.txt
llvm/lib/TextAPI/TextStub.cpp
llvm/lib/TextAPI/TextStubCommon.cpp
llvm/lib/TextAPI/TextStubCommon.h
llvm/test/Object/Inputs/tapi-v4-watchos.tbd
llvm/test/tools/llvm-tapi-diff/Inputs/v4A.tbd
llvm/test/tools/llvm-tapi-diff/Inputs/v4B.tbd
llvm/test/tools/llvm-tapi-diff/Inputs/v4C.tbd
llvm/test/tools/llvm-tapi-diff/Inputs/v4D.tbd
llvm/test/tools/llvm-tapi-diff/Inputs/v4E.tbd
llvm/unittests/TextAPI/CMakeLists.txt
llvm/unittests/TextAPI/TextStubHelpers.h
llvm/unittests/TextAPI/TextStubV4Tests.cpp

Removed: 




diff  --git a/lld/test/MachO/Inputs/libStubLink.tbd 
b/lld/test/MachO/Inputs/libStubLink.tbd
index 6e6237edbc8a2..eaf216d0a1f3f 100644
--- a/lld/test/MachO/Inputs/libStubLink.tbd
+++ b/lld/test/MachO/Inputs/libStubLink.tbd
@@ -22,3 +22,4 @@ current-version: 1.0.0
 exports:
   - targets: [ arm64-ios-simulator ]
 symbols: [ _arm64_sim_only ]
+...

diff  --git a/lld/test/MachO/invalid/invalid-stub.s 
b/lld/test/MachO/invalid/invalid-stub.s
index 997594918cd53..a80dfa6f8f15c 100644
--- a/lld/test/MachO/invalid/invalid-stub.s
+++ b/lld/test/MachO/invalid/invalid-stub.s
@@ -7,9 +7,8 @@
 # RUN: not %lld -L%t -linvalidYAML %t/test.o -o %t/test 2>&1 | FileCheck %s 
-DDIR=%t
 # RUN: not %lld -F%t -framework invalidYAML %t/test.o -o %t/test 2>&1 | 
FileCheck %s -DDIR=%t --check-prefix=CHECK-FRAMEWORK
 
-# CHECK: could not load TAPI file at [[DIR]]{{[\\/]}}libinvalidYAML.tbd: 
malformed file
-# CHECK-FRAMEWORK: could not load TAPI file at 
[[DIR]]{{[\\/]}}invalidYAML.framework{{[\\/]}}invalidYAML.tbd: malformed file
-
+# CHECK: could not load TAPI file at [[DIR]]{{[\\/]}}libinvalidYAML.tbd: 
unsupported file type  
+# CHECK-FRAMEWORK: could not load TAPI file at 
[[DIR]]{{[\\/]}}invalidYAML.framework{{[\\/]}}invalidYAML.tbd: unsupported file 
type  
 .globl _main
 _main:
   ret

diff  --git a/lld/test/MachO/tapi-link.s b/lld/test/MachO/tapi-link.s
index b2aa2f208281e..af205b6c24e99 100644
--- a/lld/test/MachO/tapi-link.s
+++ b/lld/test/MachO/tapi-link.s
@@ -121,7 +121,6 @@ exports:
 re-exports: [ 'libNested.dylib' ]
 ...
 
-## This tests that weak and thread-local symbols are imported as such.
 #--- libTlvWeak.tbd
 --- !tapi-tbd
 tbd-version:  4
@@ -131,8 +130,8 @@ uuids:
 value:----
 install-name: '/usr/lib/libTlvWeak.dylib'
 current-version:  0001.001.1
-exports:
+exports:# Validate weak & thread-local symbols 
   - targets:  [ x86_64-macos ]
 weak-symbols: [ _weak ]
 thread-local-symbols: [ _tlv ]

+...

diff  --git a/llvm/include/llvm/TextAPI/InterfaceFile.h 
b/llvm/include/llvm/TextAPI/InterfaceFile.h
index 5f07397adacaa..cb8ec4b8f76d6 100644
--- a/llvm/include/llvm/TextAPI/InterfaceFile.h
+++ b/llvm/include/llvm/TextAPI/InterfaceFile.h
@@ -66,6 +66,9 @@ enum FileType : unsigned {
   /// Text-based stub file (.tbd) version 4.0
   TBD_V4  = 1U <<  3,
 
+  /// Text-based stub file (.tbd) version 5.0
+  TBD_V5  = 1U <<  4,
+
   All = ~0U,
 
   LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),

diff  --git a/llvm/lib/TextAPI/CMakeLists.txt b/llvm/lib/TextAPI/CMakeLists.txt
index c7e9e1009ab22..1714ce33ef205 100644
--- a/llvm/lib/TextAPI/CMakeLists.txt
+++ b/llvm/lib/TextAPI/CMakeLists.txt
@@ -2,6 +2,7 @@ add_llvm_component_library(LLVMTextAPI
   Architecture.cpp
   ArchitectureSet.cpp
   InterfaceFile.cpp
+  TextStubV5.cpp
   PackedVersion.cpp
   Platform.cpp
   Symbol.cpp

diff  --git a/llvm/lib/TextAPI/TextStub.cpp b/llvm/lib/TextAPI/TextStub.cpp
index ff93e43356f73..73cb61472bff3 100644
--- a/llvm/lib/TextAPI/TextStub.cpp
+++ b/llvm/lib/TextAPI/TextStub.cpp
@@ -258,16 +258,6 @@ struct UUIDv4 {
   UUIDv4(const Target &TargetID, const std::string &Value)
   : TargetID(TargetID), Value(Value) {}
 };
-
-// clang-format off
-enum TBDFlags : unsigned {
-  None = 0U,
-  FlatNamespace= 1U << 0,
-  NotApplicationExtensi

[llvm-branch-commits] [llvm] 79aeef6 - [TextAPI] Capture new properties from TBD to InterfaceFile

2023-02-16 Thread Cyndy Ishida via llvm-branch-commits

Author: Cyndy Ishida
Date: 2023-02-16T13:52:12-08:00
New Revision: 79aeef6900dbdf683f44499e9cc83f683f3bfbf7

URL: 
https://github.com/llvm/llvm-project/commit/79aeef6900dbdf683f44499e9cc83f683f3bfbf7
DIFF: 
https://github.com/llvm/llvm-project/commit/79aeef6900dbdf683f44499e9cc83f683f3bfbf7.diff

LOG: [TextAPI] Capture new properties from TBD to InterfaceFile

* Deployment Versions for targets
* Run Search Paths
* Text vs Data Segment attributes to symbols

Differential Revision: https://reviews.llvm.org/D144158

Added: 


Modified: 
llvm/include/llvm/TextAPI/InterfaceFile.h
llvm/include/llvm/TextAPI/Symbol.h
llvm/include/llvm/TextAPI/Target.h
llvm/lib/TextAPI/InterfaceFile.cpp
llvm/lib/TextAPI/Target.cpp
llvm/lib/TextAPI/TextStubV5.cpp
llvm/unittests/TextAPI/TextStubHelpers.h
llvm/unittests/TextAPI/TextStubV3Tests.cpp
llvm/unittests/TextAPI/TextStubV4Tests.cpp
llvm/unittests/TextAPI/TextStubV5Tests.cpp

Removed: 




diff  --git a/llvm/include/llvm/TextAPI/InterfaceFile.h 
b/llvm/include/llvm/TextAPI/InterfaceFile.h
index cb8ec4b8f76d6..c32917da69f8a 100644
--- a/llvm/include/llvm/TextAPI/InterfaceFile.h
+++ b/llvm/include/llvm/TextAPI/InterfaceFile.h
@@ -348,6 +348,18 @@ class InterfaceFile {
 return Documents;
   }
 
+  /// Set the runpath search paths.
+  /// \param InputTarget The target applicable to runpath search path.
+  /// \param RPath The name of runpath.
+  void addRPath(const Target &InputTarget, StringRef RPath);
+
+  /// Get the list of runpath search paths.
+  ///
+  /// \return Returns a list of the rpaths per target.
+  const std::vector> &rpaths() const {
+return RPaths;
+  }
+
   /// Add a symbol to the symbols list or extend an existing one.
   void addSymbol(SymbolKind Kind, StringRef Name, const TargetList &Targets,
  SymbolFlags Flags = SymbolFlags::None);
@@ -435,6 +447,7 @@ class InterfaceFile {
   std::vector ReexportedLibraries;
   std::vector> Documents;
   std::vector> UUIDs;
+  std::vector> RPaths;
   SymbolMapType Symbols;
   InterfaceFile *Parent = nullptr;
 };

diff  --git a/llvm/include/llvm/TextAPI/Symbol.h 
b/llvm/include/llvm/TextAPI/Symbol.h
index 1c25295b299d3..6cb462ad403b3 100644
--- a/llvm/include/llvm/TextAPI/Symbol.h
+++ b/llvm/include/llvm/TextAPI/Symbol.h
@@ -40,7 +40,13 @@ enum class SymbolFlags : uint8_t {
   /// Rexported
   Rexported= 1U << 4,
 
-  LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Rexported),
+  /// Data Segment  
+  Data = 1U << 5,
+
+  /// Text Segment
+  Text = 1U << 6,
+  
+  LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Text),
 };
 
 // clang-format on
@@ -93,6 +99,14 @@ class Symbol {
 return (Flags & SymbolFlags::Rexported) == SymbolFlags::Rexported;
   }
 
+  bool isData() const {
+return (Flags & SymbolFlags::Data) == SymbolFlags::Data;
+  }
+
+  bool isText() const {
+return (Flags & SymbolFlags::Text) == SymbolFlags::Text;
+  }
+
   using const_target_iterator = TargetList::const_iterator;
   using const_target_range = llvm::iterator_range;
   const_target_range targets() const { return {Targets}; }

diff  --git a/llvm/include/llvm/TextAPI/Target.h 
b/llvm/include/llvm/TextAPI/Target.h
index fbb76295f706a..dc0e4f92ae802 100644
--- a/llvm/include/llvm/TextAPI/Target.h
+++ b/llvm/include/llvm/TextAPI/Target.h
@@ -10,6 +10,8 @@
 #define LLVM_TEXTAPI_TARGET_H
 
 #include "llvm/Support/Error.h"
+#include "llvm/Support/VersionTuple.h"
+#include "llvm/TargetParser/Triple.h"
 #include "llvm/TextAPI/Architecture.h"
 #include "llvm/TextAPI/ArchitectureSet.h"
 #include "llvm/TextAPI/Platform.h"
@@ -26,10 +28,12 @@ namespace MachO {
 class Target {
 public:
   Target() = default;
-  Target(Architecture Arch, PlatformType Platform)
-  : Arch(Arch), Platform(Platform) {}
+  Target(Architecture Arch, PlatformType Platform,
+ VersionTuple MinDeployment = {})
+  : Arch(Arch), Platform(Platform), MinDeployment(MinDeployment) {}
   explicit Target(const llvm::Triple &Triple)
-  : Arch(mapToArchitecture(Triple)), Platform(mapToPlatformType(Triple)) {}
+  : Arch(mapToArchitecture(Triple)), Platform(mapToPlatformType(Triple)),
+MinDeployment(Triple.getOSVersion()) {}
 
   static llvm::Expected create(StringRef Target);
 
@@ -37,6 +41,7 @@ class Target {
 
   Architecture Arch;
   PlatformType Platform;
+  VersionTuple MinDeployment;
 };
 
 inline bool operator==(const Target &LHS, const Target &RHS) {

diff  --git a/llvm/lib/TextAPI/InterfaceFile.cpp 
b/llvm/lib/TextAPI/InterfaceFile.cpp
index 1156a39228e7a..f9b5e95d5f6fb 100644
--- a/llvm/lib/TextAPI/InterfaceFile.cpp
+++ b/llvm/lib/TextAPI/InterfaceFile.cpp
@@ -71,6 +71,19 @@ void InterfaceFile::addParentUmbrella(const Target &Target_, 
StringRef Parent) {
   ParentUmbrellas.emplace(Iter, Target_, std::string(Parent));
 }
 
+void InterfaceFile::addRPath(const Target &InputTarget, St

[llvm-branch-commits] [llvm] 7f25460 - [text] WIP writerf

2023-02-16 Thread Cyndy Ishida via llvm-branch-commits

Author: Cyndy Ishida
Date: 2023-02-16T13:53:38-08:00
New Revision: 7f254605c66e0260f8b169cd62b094e8a56ddb70

URL: 
https://github.com/llvm/llvm-project/commit/7f254605c66e0260f8b169cd62b094e8a56ddb70
DIFF: 
https://github.com/llvm/llvm-project/commit/7f254605c66e0260f8b169cd62b094e8a56ddb70.diff

LOG: [text] WIP writerf

Added: 


Modified: 
llvm/lib/TextAPI/TextStub.cpp
llvm/lib/TextAPI/TextStubCommon.h
llvm/lib/TextAPI/TextStubV5.cpp
llvm/unittests/TextAPI/TextStubV5Tests.cpp

Removed: 




diff  --git a/llvm/lib/TextAPI/TextStub.cpp b/llvm/lib/TextAPI/TextStub.cpp
index 73cb61472bff..af60bd794e2d 100644
--- a/llvm/lib/TextAPI/TextStub.cpp
+++ b/llvm/lib/TextAPI/TextStub.cpp
@@ -1163,6 +1163,12 @@ Error TextAPIWriter::writeToStream(raw_ostream &OS, 
const InterfaceFile &File) {
   TextAPIContext Ctx;
   Ctx.Path = std::string(File.getPath());
   Ctx.FileKind = File.getFileType();
+
+  // Write out in JSON format.
+  if (Ctx.FileKind >= FileType::TBD_V5) {
+return serializeInterfaceFileToJSON(OS, File);
+  }
+
   llvm::yaml::Output YAMLOut(OS, &Ctx, /*WrapColumn=*/80);
 
   std::vector Files;

diff  --git a/llvm/lib/TextAPI/TextStubCommon.h 
b/llvm/lib/TextAPI/TextStubCommon.h
index 51b423137f4e..6ee5b3931831 100644
--- a/llvm/lib/TextAPI/TextStubCommon.h
+++ b/llvm/lib/TextAPI/TextStubCommon.h
@@ -45,6 +45,8 @@ class PackedVersion;
 
 Expected>
 getInterfaceFileFromJSON(StringRef JSON);
+
+Error serializeInterfaceFileToJSON(raw_ostream &OS, const InterfaceFile &File);
 } // namespace MachO
 
 namespace yaml {

diff  --git a/llvm/lib/TextAPI/TextStubV5.cpp b/llvm/lib/TextAPI/TextStubV5.cpp
index d19f7dd457f3..abd3f6030e96 100644
--- a/llvm/lib/TextAPI/TextStubV5.cpp
+++ b/llvm/lib/TextAPI/TextStubV5.cpp
@@ -155,6 +155,10 @@ static llvm::SmallString<128> getParseErrorMsg(TBDKey Key) 
{
   return {"invalid ", Keys[Key], " section"};
 }
 
+static llvm::SmallString<128> getSerializeErrorMsg(TBDKey Key) {
+  return {"missing ", Keys[Key], " information"};
+}
+
 class JSONStubError : public llvm::ErrorInfo {
 public:
   JSONStubError(Twine ErrMsg) : Message(ErrMsg.str()) {}
@@ -710,3 +714,78 @@ MachO::getInterfaceFileFromJSON(StringRef JSON) {
   }
   return std::move(IF);
 }
+
+namespace {
+
+bool insertNonEmptyArray(Object &Obj, TBDKey Key, Array &&Contents) {
+  if (Contents.empty())
+return false;
+  Obj[Keys[Key]] = std::move(Contents);
+  return true;
+}
+
+Array serializeTargets(const InterfaceFile *File) {
+  Array Targets;
+  for (const auto Targ : File->targets()) {
+Object TargetInfo;
+TargetInfo[Keys[TBDKey::Deployment]] = Targ.MinDeployment.getAsString();
+TargetInfo[Keys[TBDKey::Target]] =
+(getArchitectureName(Targ.Arch) + "-" + getPlatformName(Targ.Platform))
+.str();
+Targets.emplace_back(std::move(TargetInfo));
+  }
+  return Targets;
+}
+
+Array serializeInstallNames(const InterfaceFile *File) {
+  Array Names;
+  return Names;
+}
+
+Expected serializeIF(const InterfaceFile *File) {
+  Object Library;
+
+  // Handle required keys.
+  if (!insertNonEmptyArray(Library, TBDKey::TargetInfo, 
serializeTargets(File)))
+return make_error(getSerializeErrorMsg(TBDKey::TargetInfo));
+
+  // if (!insertNonEmptyArray(Library, TBDKey::InstallName,
+  // serializeInstallNames(File)))
+  //   return
+  //   make_error(getSerializeErrorMsg(TBDKey::InstallName));
+
+  return Library;
+}
+
+Expected getJSON(const InterfaceFile *File) {
+  assert(File->getFileType() == FileType::TBD_V5 &&
+ "unexpected json file format version");
+  Object Root;
+
+  Array Documents;
+  auto MainLibOrErr = serializeIF(File);
+  if (!MainLibOrErr)
+return MainLibOrErr;
+  Documents.emplace_back(std::move(*MainLibOrErr));
+  for (const auto &Doc : File->documents()) {
+auto LibOrErr = serializeIF(Doc.get());
+if (!LibOrErr)
+  return LibOrErr;
+Documents.emplace_back(std::move(*LibOrErr));
+  }
+
+  Root[Keys[TBDKey::TBDVersion]] = 5;
+  Root[Keys[TBDKey::Document]] = std::move(Documents);
+  return Root;
+}
+
+} // namespace
+
+Error MachO::serializeInterfaceFileToJSON(raw_ostream &OS,
+  const InterfaceFile &File) {
+  auto TextFile = getJSON(&File);
+  if (!TextFile)
+return TextFile.takeError();
+  OS << formatv("{0:2}", Value(std::move(*TextFile))) << "\n";
+  return Error::success();
+}

diff  --git a/llvm/unittests/TextAPI/TextStubV5Tests.cpp 
b/llvm/unittests/TextAPI/TextStubV5Tests.cpp
index 5b0b596b1cd1..8ccde0c79483 100644
--- a/llvm/unittests/TextAPI/TextStubV5Tests.cpp
+++ b/llvm/unittests/TextAPI/TextStubV5Tests.cpp
@@ -519,4 +519,154 @@ TEST(TBDv5, ReadMultipleDocuments) {
   std::equal(Exports.begin(), Exports.end(), std::begin(ExpectedExports)));
 }
 
+TEST(TBDv5, WriteFile) {
+  static const char TBDv5File[] = R"({ 
+"tapi_tbd_version": 5,
+"files": [
+{
+"target_info": [
+