[clang] [llvm] [RISCV] Add back SiFive's cdiscard.d.l1 and cflush.d.l1 instructions. (PR #83896)

2024-03-11 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @topperc, can you add instruction alias for cflush and cdiscard instructions 
when the rs1 is X0 to `sf.cflush.d.l1`  and `sf.cflush.d.l1` respectively, as 
this register is optional according to spec? 

https://github.com/llvm/llvm-project/pull/83896
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [RISCV] Add back SiFive's cdiscard.d.l1 and cflush.d.l1 instructions. (PR #83896)

2024-03-11 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Also, I think we might need to update the extensions in the 
`RISCVProcessors.td` file under SIFIVE_S76 microcontroller?

https://github.com/llvm/llvm-project/pull/83896
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [RISCV] Add back SiFive's cdiscard.d.l1 and cflush.d.l1 instructions. (PR #83896)

2024-03-11 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

> > Hi @topperc, can you add instruction alias for cflush and cdiscard 
> > instructions when the rs1 is X0 to `sf.cflush.d.l1` and `sf.cflush.d.l1` 
> > respectively, as this register is optional according to spec?
> 
> x0 has special meaning, but the spec never says it is "optional".

Possibly I am mistaken here but this is what I inferred from the below text in 
the mannual - rs1 is optional, so if a user does not specify it then by default 
it will be X0. Apologies if I am missing something here but just wanted to be 
clear.

>From the mannual - 
`Opcode 0xFC73, with **optional** rs1 field in bits [19:15]`

https://github.com/llvm/llvm-project/pull/83896
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [RISCV] Add back SiFive's cdiscard.d.l1 and cflush.d.l1 instructions. (PR #83896)

2024-03-11 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

> > Also, I think we might need to update the extensions in the 
> > `RISCVProcessors.td` file under SIFIVE_S76 microcontroller?
> 
> This is a M-mode only extension, and we haven't historically been adding M or 
> S mode extensions to the -mcpu lists. Except for `xiangshan-nanhu` having 
> `Svinval`. But maybe there's a microcontroller vs application core 
> distinction we should be making?

I see, I was not aware of this. Thanks for your reply

https://github.com/llvm/llvm-project/pull/83896
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [RISCV] Add back SiFive's cdiscard.d.l1 and cflush.d.l1 instructions. (PR #83896)

2024-03-12 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Thanks for clearing the confusion around whether rs1 would be optional or not. 
Can we also add lit tests for the aliases? 

https://github.com/llvm/llvm-project/pull/83896
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [RISCV] Add back SiFive's cdiscard.d.l1 and cflush.d.l1 instructions. (PR #83896)

2024-03-12 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Thanks for the prompt reply and latest patchset.

1. Do we need to add documentation in RISCVUsage.rst file for xsfcease?
2. Also, as we are adding cease instruction in this PR, can we rename the PR to 
include the cease instruction as well?

https://github.com/llvm/llvm-project/pull/83896
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [RISCV] Add back SiFive's cdiscard.d.l1, cflush.d.l1, and cease instructions. (PR #83896)

2024-03-13 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

I believe the patch is ready to be merged now, but since I am not the reviewer 
here, someone else must approve it as well.

Thanks again for the patch!

https://github.com/llvm/llvm-project/pull/83896
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-23 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

ping! for merging this PR

https://github.com/llvm/llvm-project/pull/81727
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV ba… (PR #81727)

2024-02-14 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/81727

The below culprit patch enabled the generation of asynchronous unwind tables 
(-funwind-tables=2) by default for RISCV for both linux and RISCVToolChain 
baremetal object. However, since there are 2 baremetal toolchain objects for 
RISCV, this created a discrepancy between their behavior. Moreover, enabling 
the generation of asynchronous unwind tables based on whether `-gcc-toolchain` 
option is present or not doesn't seem to be the best criteria to decide on the 
same. This patch make the behavior consistent by disabling the unwind tables in 
RISCVToolChain Baremetal object.

Culprit Patch - https://reviews.llvm.org/D145164


>From 42211bccd8ffc60751234ab5695be135ea8b6373 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 14 Feb 2024 03:00:20 -0800
Subject: [PATCH] [RISCV] Disable generation of asynchronous unwind tables for
 RISCV baremetal

The below culprit patch enabled the generation of asynchronous unwind tables
(-funwind-tables=2) by default for RISCV for both linux and RISCVToolChain
baremetal object. However, since there are 2 baremetal toolchain objects for
RISCV, this created a discrepancy between their behavior. Moreover, enabling
the generation of asynchronous unwind tables based on whether `-gcc-toolchain`
option is present or not doesn't seem to be the best criteria to decide on
the same.

Culprit Patch - https://reviews.llvm.org/D145164
---
 clang/lib/Driver/ToolChains/RISCVToolchain.cpp | 5 +
 clang/lib/Driver/ToolChains/RISCVToolchain.h   | 2 ++
 clang/test/Driver/clang-translation.c  | 8 
 3 files changed, 15 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp 
b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
index 85beb945cbf6fc..624099d21ae124 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
@@ -86,6 +86,11 @@ RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList 
&Args) const {
   return ToolChain::UNW_None;
 }
 
+ToolChain::UnwindTableLevel RISCVToolChain::getDefaultUnwindTableLevel(
+const llvm::opt::ArgList &Args) const {
+  return UnwindTableLevel::None;
+}
+
 void RISCVToolChain::addClangTargetOptions(
 const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.h 
b/clang/lib/Driver/ToolChains/RISCVToolchain.h
index cec817ef7190be..fa0aa265d842bb 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.h
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.h
@@ -28,6 +28,8 @@ class LLVM_LIBRARY_VISIBILITY RISCVToolChain : public 
Generic_ELF {
   RuntimeLibType GetDefaultRuntimeLibType() const override;
   UnwindLibType
   GetUnwindLibType(const llvm::opt::ArgList &Args) const override;
+  UnwindTableLevel
+  getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
   void
   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args) const override;
diff --git a/clang/test/Driver/clang-translation.c 
b/clang/test/Driver/clang-translation.c
index a7343ea18b2135..64d55f1b88ea6d 100644
--- a/clang/test/Driver/clang-translation.c
+++ b/clang/test/Driver/clang-translation.c
@@ -421,3 +421,11 @@
 // MIPSN32R6EL: "-target-cpu" "mips64r6"
 // MIPSN32R6EL: "-target-abi" "n32"
 // MIPSN32R6EL: "-mfloat-abi" "hard"
+
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+//
+// UWTABLE: "-funwind-tables=2"
+// NOUWTABLE-NOT: "-funwind-tables=2"
\ No newline at end of file

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


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV ba… (PR #81727)

2024-02-14 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi
@jonathonpenix  @apazos @kito-cheng @asb @MaskRay - Requesting for adding as 
reviewers to review the PR. Thanks in advance!

https://github.com/llvm/llvm-project/pull/81727
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-16 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @asb, since Petr already gave a good-ahead to this patch in the meeting, let 
me know if we can merge this PR if there are no new changes need to be made

https://github.com/llvm/llvm-project/pull/81727
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-20 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/81727

>From 42211bccd8ffc60751234ab5695be135ea8b6373 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 14 Feb 2024 03:00:20 -0800
Subject: [PATCH 1/2] [RISCV] Disable generation of asynchronous unwind tables
 for RISCV baremetal

The below culprit patch enabled the generation of asynchronous unwind tables
(-funwind-tables=2) by default for RISCV for both linux and RISCVToolChain
baremetal object. However, since there are 2 baremetal toolchain objects for
RISCV, this created a discrepancy between their behavior. Moreover, enabling
the generation of asynchronous unwind tables based on whether `-gcc-toolchain`
option is present or not doesn't seem to be the best criteria to decide on
the same.

Culprit Patch - https://reviews.llvm.org/D145164
---
 clang/lib/Driver/ToolChains/RISCVToolchain.cpp | 5 +
 clang/lib/Driver/ToolChains/RISCVToolchain.h   | 2 ++
 clang/test/Driver/clang-translation.c  | 8 
 3 files changed, 15 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp 
b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
index 85beb945cbf6fc..624099d21ae124 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
@@ -86,6 +86,11 @@ RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList 
&Args) const {
   return ToolChain::UNW_None;
 }
 
+ToolChain::UnwindTableLevel RISCVToolChain::getDefaultUnwindTableLevel(
+const llvm::opt::ArgList &Args) const {
+  return UnwindTableLevel::None;
+}
+
 void RISCVToolChain::addClangTargetOptions(
 const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.h 
b/clang/lib/Driver/ToolChains/RISCVToolchain.h
index cec817ef7190be..fa0aa265d842bb 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.h
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.h
@@ -28,6 +28,8 @@ class LLVM_LIBRARY_VISIBILITY RISCVToolChain : public 
Generic_ELF {
   RuntimeLibType GetDefaultRuntimeLibType() const override;
   UnwindLibType
   GetUnwindLibType(const llvm::opt::ArgList &Args) const override;
+  UnwindTableLevel
+  getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
   void
   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args) const override;
diff --git a/clang/test/Driver/clang-translation.c 
b/clang/test/Driver/clang-translation.c
index a7343ea18b2135..64d55f1b88ea6d 100644
--- a/clang/test/Driver/clang-translation.c
+++ b/clang/test/Driver/clang-translation.c
@@ -421,3 +421,11 @@
 // MIPSN32R6EL: "-target-cpu" "mips64r6"
 // MIPSN32R6EL: "-target-abi" "n32"
 // MIPSN32R6EL: "-mfloat-abi" "hard"
+
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+//
+// UWTABLE: "-funwind-tables=2"
+// NOUWTABLE-NOT: "-funwind-tables=2"
\ No newline at end of file

>From 559f1d6e4221dc94cd035c0f568dea630dcb6f37 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 14 Feb 2024 03:00:20 -0800
Subject: [PATCH 2/2] [RISCV] Disable generation of asynchronous unwind tables
 for RISCV baremetal

The below culprit patch enabled the generation of asynchronous unwind tables
(-funwind-tables=2) by default for RISCV for both linux and RISCVToolChain
baremetal object. However, since there are 2 baremetal toolchain objects for
RISCV, this created a discrepancy between their behavior. Moreover, enabling
the generation of asynchronous unwind tables based on whether `-gcc-toolchain`
option is present or not doesn't seem to be the best criteria to decide on
the same.

Culprit Patch - https://reviews.llvm.org/D145164
---
 clang/test/Driver/riscv-features.c | 8 
 1 file changed, 8 insertions(+)

diff --git a/clang/test/Driver/riscv-features.c 
b/clang/test/Driver/riscv-features.c
index d3700f71aa7e1d..15832e79d2a491 100644
--- a/clang/test/Driver/riscv-features.c
+++ b/clang/test/Driver/riscv-features.c
@@ -35,6 +35,14 @@
 // FAST-UNALIGNED-ACCESS: "-target-feature" "+fast-unaligned-access"
 // NO-FAST-UNALIGNED-ACCESS: "-target-feature" "-fast-unaligned-access"
 
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" -#

[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-20 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/81727

>From 42211bccd8ffc60751234ab5695be135ea8b6373 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 14 Feb 2024 03:00:20 -0800
Subject: [PATCH] [RISCV] Disable generation of asynchronous unwind tables for
 RISCV baremetal

The below culprit patch enabled the generation of asynchronous unwind tables
(-funwind-tables=2) by default for RISCV for both linux and RISCVToolChain
baremetal object. However, since there are 2 baremetal toolchain objects for
RISCV, this created a discrepancy between their behavior. Moreover, enabling
the generation of asynchronous unwind tables based on whether `-gcc-toolchain`
option is present or not doesn't seem to be the best criteria to decide on
the same.

Culprit Patch - https://reviews.llvm.org/D145164
---
 clang/lib/Driver/ToolChains/RISCVToolchain.cpp | 5 +
 clang/lib/Driver/ToolChains/RISCVToolchain.h   | 2 ++
 clang/test/Driver/clang-translation.c  | 8 
 3 files changed, 15 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp 
b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
index 85beb945cbf6fc..624099d21ae124 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
@@ -86,6 +86,11 @@ RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList 
&Args) const {
   return ToolChain::UNW_None;
 }
 
+ToolChain::UnwindTableLevel RISCVToolChain::getDefaultUnwindTableLevel(
+const llvm::opt::ArgList &Args) const {
+  return UnwindTableLevel::None;
+}
+
 void RISCVToolChain::addClangTargetOptions(
 const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.h 
b/clang/lib/Driver/ToolChains/RISCVToolchain.h
index cec817ef7190be..fa0aa265d842bb 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.h
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.h
@@ -28,6 +28,8 @@ class LLVM_LIBRARY_VISIBILITY RISCVToolChain : public 
Generic_ELF {
   RuntimeLibType GetDefaultRuntimeLibType() const override;
   UnwindLibType
   GetUnwindLibType(const llvm::opt::ArgList &Args) const override;
+  UnwindTableLevel
+  getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
   void
   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args) const override;
diff --git a/clang/test/Driver/clang-translation.c 
b/clang/test/Driver/clang-translation.c
index a7343ea18b2135..64d55f1b88ea6d 100644
--- a/clang/test/Driver/clang-translation.c
+++ b/clang/test/Driver/clang-translation.c
@@ -421,3 +421,11 @@
 // MIPSN32R6EL: "-target-cpu" "mips64r6"
 // MIPSN32R6EL: "-target-abi" "n32"
 // MIPSN32R6EL: "-mfloat-abi" "hard"
+
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+//
+// UWTABLE: "-funwind-tables=2"
+// NOUWTABLE-NOT: "-funwind-tables=2"
\ No newline at end of file

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


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-20 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/81727

>From 8cc7535ed4c05d4cf14cdedc26077eccf06c8d53 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 14 Feb 2024 03:00:20 -0800
Subject: [PATCH] [RISCV] Disable generation of asynchronous unwind tables for
 RISCV baremetal

The below culprit patch enabled the generation of asynchronous unwind tables
(-funwind-tables=2) by default for RISCV for both linux and RISCVToolChain
baremetal object. However, since there are 2 baremetal toolchain objects for
RISCV, this created a discrepancy between their behavior. Moreover, enabling
the generation of asynchronous unwind tables based on whether `-gcc-toolchain`
option is present or not doesn't seem to be the best criteria to decide on
the same.

Culprit Patch - https://reviews.llvm.org/D145164
---
 clang/lib/Driver/ToolChains/RISCVToolchain.cpp | 5 +
 clang/lib/Driver/ToolChains/RISCVToolchain.h   | 2 ++
 clang/test/Driver/riscv-features.c | 8 
 3 files changed, 15 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp 
b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
index 85beb945cbf6fc..624099d21ae124 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
@@ -86,6 +86,11 @@ RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList 
&Args) const {
   return ToolChain::UNW_None;
 }
 
+ToolChain::UnwindTableLevel RISCVToolChain::getDefaultUnwindTableLevel(
+const llvm::opt::ArgList &Args) const {
+  return UnwindTableLevel::None;
+}
+
 void RISCVToolChain::addClangTargetOptions(
 const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.h 
b/clang/lib/Driver/ToolChains/RISCVToolchain.h
index cec817ef7190be..fa0aa265d842bb 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.h
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.h
@@ -28,6 +28,8 @@ class LLVM_LIBRARY_VISIBILITY RISCVToolChain : public 
Generic_ELF {
   RuntimeLibType GetDefaultRuntimeLibType() const override;
   UnwindLibType
   GetUnwindLibType(const llvm::opt::ArgList &Args) const override;
+  UnwindTableLevel
+  getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
   void
   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args) const override;
diff --git a/clang/test/Driver/riscv-features.c 
b/clang/test/Driver/riscv-features.c
index d3700f71aa7e1d..15832e79d2a491 100644
--- a/clang/test/Driver/riscv-features.c
+++ b/clang/test/Driver/riscv-features.c
@@ -35,6 +35,14 @@
 // FAST-UNALIGNED-ACCESS: "-target-feature" "+fast-unaligned-access"
 // NO-FAST-UNALIGNED-ACCESS: "-target-feature" "-fast-unaligned-access"
 
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+//
+// UWTABLE: "-funwind-tables=2"
+// NOUWTABLE-NOT: "-funwind-tables=2"
+
 // RUN: %clang --target=riscv32-linux -### %s -fsyntax-only 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=DEFAULT-LINUX
 // RUN: %clang --target=riscv64-linux -### %s -fsyntax-only 2>&1 \

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


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-20 Thread Garvit Gupta via cfe-commits


@@ -421,3 +421,11 @@
 // MIPSN32R6EL: "-target-cpu" "mips64r6"
 // MIPSN32R6EL: "-target-abi" "n32"
 // MIPSN32R6EL: "-mfloat-abi" "hard"
+
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE

quic-garvgupt wrote:

Done

https://github.com/llvm/llvm-project/pull/81727
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-20 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @asb, I do not have write access,  requesting to merge this PR on my behalf. 
Also, requesting to backport this PR onto the release  branch. Thanks in 
advance!

https://github.com/llvm/llvm-project/pull/81727
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-20 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/81727

>From 0d1f9be389a41ba2f1531bf13e8d1f14aadf9eeb Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 14 Feb 2024 03:00:20 -0800
Subject: [PATCH] [RISCV] Disable generation of asynchronous unwind tables for
 RISCV baremetal

The below culprit patch enabled the generation of asynchronous unwind tables
(-funwind-tables=2) by default for RISCV for both linux and RISCVToolChain
baremetal object. However, since there are 2 baremetal toolchain objects for
RISCV, this created a discrepancy between their behavior. Moreover, enabling
the generation of asynchronous unwind tables based on whether `-gcc-toolchain`
option is present or not doesn't seem to be the best criteria to decide on
the same.

Culprit Patch - https://reviews.llvm.org/D145164
---
 clang/lib/Driver/ToolChains/RISCVToolchain.cpp | 5 +
 clang/lib/Driver/ToolChains/RISCVToolchain.h   | 2 ++
 clang/test/Driver/riscv-features.c | 8 
 3 files changed, 15 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp 
b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
index 85beb945cbf6fc..624099d21ae124 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.cpp
@@ -86,6 +86,11 @@ RISCVToolChain::GetUnwindLibType(const llvm::opt::ArgList 
&Args) const {
   return ToolChain::UNW_None;
 }
 
+ToolChain::UnwindTableLevel RISCVToolChain::getDefaultUnwindTableLevel(
+const llvm::opt::ArgList &Args) const {
+  return UnwindTableLevel::None;
+}
+
 void RISCVToolChain::addClangTargetOptions(
 const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args,
diff --git a/clang/lib/Driver/ToolChains/RISCVToolchain.h 
b/clang/lib/Driver/ToolChains/RISCVToolchain.h
index cec817ef7190be..fa0aa265d842bb 100644
--- a/clang/lib/Driver/ToolChains/RISCVToolchain.h
+++ b/clang/lib/Driver/ToolChains/RISCVToolchain.h
@@ -28,6 +28,8 @@ class LLVM_LIBRARY_VISIBILITY RISCVToolChain : public 
Generic_ELF {
   RuntimeLibType GetDefaultRuntimeLibType() const override;
   UnwindLibType
   GetUnwindLibType(const llvm::opt::ArgList &Args) const override;
+  UnwindTableLevel
+  getDefaultUnwindTableLevel(const llvm::opt::ArgList &Args) const override;
   void
   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringList &CC1Args) const override;
diff --git a/clang/test/Driver/riscv-features.c 
b/clang/test/Driver/riscv-features.c
index a108383e29fb6b..fc5fb0f27e3af4 100644
--- a/clang/test/Driver/riscv-features.c
+++ b/clang/test/Driver/riscv-features.c
@@ -41,6 +41,14 @@
 // FAST-UNALIGNED-ACCESS: "-target-feature" "+fast-unaligned-access"
 // NO-FAST-UNALIGNED-ACCESS: "-target-feature" "-fast-unaligned-access"
 
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv32-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" -### %s 2>&1 | 
FileCheck %s -check-prefix=NOUWTABLE
+// RUN: %clang --target=riscv64-unknown-elf --gcc-toolchain="" 
-fasynchronous-unwind-tables -### %s 2>&1 | FileCheck %s -check-prefix=UWTABLE
+//
+// UWTABLE: "-funwind-tables=2"
+// NOUWTABLE-NOT: "-funwind-tables=2"
+
 // RUN: %clang --target=riscv32-linux -### %s -fsyntax-only 2>&1 \
 // RUN:   | FileCheck %s -check-prefix=DEFAULT-LINUX
 // RUN: %clang --target=riscv64-linux -### %s -fsyntax-only 2>&1 \

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


[clang] [RISCV] Disable generation of asynchronous unwind tables for RISCV baremetal (PR #81727)

2024-02-21 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @MaskRay, please review the updated changes and if they are fine, please 
merge this PR on my behalf. Thanks!

https://github.com/llvm/llvm-project/pull/81727
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-05 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/118809

Currently, LLVM has two RISC-V toolchain classes in Clang for baremetal 
development, creating unnecessary maintenance overhead. This patch extends the 
BareMetal toolchain to support an existing GCC installation, resolving this 
issue.

The latest patchset preserves the behavior of both toolchain objects with minor 
differences. If no --sysroot option is passed on the command line or if the GCC 
installation is invalid, the sysroot will first be formed as per the 
RISCVToolChain baremetal object. If this path does not exist, the sysroot will 
be formed as per the BareMetal toolchain object.

Additionally, the presence of --gcc-toolchain or --gcc-install-dir will imply 
that GNU linker is the default linker unless otherwise a differnt linker is 
passed through `-fuse-ld` flag.

RFC - 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

>From b1bd1ee4f3a4966b50e0b35fd349091660e84576 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 13 Nov 2024 02:45:51 -0800
Subject: [PATCH] [RISCV] Merging RISCVToolChain and BareMetal toolchains

Currently, LLVM has two RISC-V toolchain classes in Clang for baremetal
development, creating unnecessary maintenance overhead. This patch extends the
BareMetal toolchain to support an existing GCC installation, resolving this 
issue.

The latest patchset preserves the behavior of both toolchain objects with minor
differences. If no --sysroot option is passed on the command line or if the GCC
installation is invalid, the sysroot will first be formed as per the
RISCVToolChain baremetal object. If this path does not exist, the sysroot will
be formed as per the BareMetal toolchain object.

Additionally, the presence of --gcc-toolchain or --gcc-install-dir will imply
that GNU linker is the default linker unless otherwise a differnt linker is
passed through `-fuse-ld` flag.

RFC - 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
---
 clang/lib/Driver/Driver.cpp |   4 -
 clang/lib/Driver/ToolChains/BareMetal.cpp   | 225 
 clang/lib/Driver/ToolChains/BareMetal.h |  34 ++-
 clang/test/Driver/arm-gnutools.c|  12 ++
 clang/test/Driver/baremetal-multilib.yaml   |   4 +-
 clang/test/Driver/baremetal-sysroot.cpp |   4 +-
 clang/test/Driver/baremetal.cpp | 123 ++-
 clang/test/Driver/riscv-args.c  |   2 +-
 clang/test/Driver/riscv32-toolchain-extra.c |   4 +-
 clang/test/Driver/riscv32-toolchain.c   |   6 +-
 clang/test/Driver/riscv64-toolchain-extra.c |   4 +-
 clang/test/Driver/riscv64-toolchain.c   |   4 +-
 12 files changed, 314 insertions(+), 112 deletions(-)
 create mode 100644 clang/test/Driver/arm-gnutools.c

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 7de8341b8d2d61..c5185ccedd6201 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6521,10 +6521,6 @@ const ToolChain &Driver::getToolChain(const ArgList 
&Args,
 break;
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
-if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
-  TC =
-  std::make_unique(*this, Target, 
Args);
-else
   TC = std::make_unique(*this, Target, Args);
 break;
   case llvm::Triple::ve:
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index f9a73f60973e4c..1d065562e9a6ef 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -97,7 +97,8 @@ static bool findRISCVMultilibs(const Driver &D,
   return false;
 }
 
-static std::string computeBaseSysRoot(const Driver &D, bool IncludeTriple) {
+static std::string computeInstalledToolchainSysRoot(const Driver &D,
+bool IncludeTriple) {
   if (!D.SysRoot.empty())
 return D.SysRoot;
 
@@ -110,20 +111,94 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+// GCC sysroot here means form sysroot from either --gcc-install-dir, or from
+// --gcc-toolchain or if the toolchain is installed alongside clang in
+// bin/../ directory if it is not explicitly specified on the 
command
+//  line through `--sysroot` option. libc here will be newlib.
+std::string BareMetal::computeGCCSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+return getDriver().SysRoot;
+
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain e

[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-05 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/118809

>From b74795d44846b79252bd3fb1f1e8179311725c74 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 13 Nov 2024 02:45:51 -0800
Subject: [PATCH] [RISCV] Merging RISCVToolChain and BareMetal toolchains

Currently, LLVM has two RISC-V toolchain classes in Clang for baremetal
development, creating unnecessary maintenance overhead. This patch extends the
BareMetal toolchain to support an existing GCC installation, resolving this 
issue.

The latest patchset preserves the behavior of both toolchain objects with minor
differences. If no --sysroot option is passed on the command line or if the GCC
installation is invalid, the sysroot will first be formed as per the
RISCVToolChain baremetal object. If this path does not exist, the sysroot will
be formed as per the BareMetal toolchain object.

Additionally, the presence of --gcc-toolchain or --gcc-install-dir will imply
that GNU linker is the default linker unless otherwise a differnt linker is
passed through `-fuse-ld` flag.

RFC - 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
---
 clang/lib/Driver/Driver.cpp |   6 +-
 clang/lib/Driver/ToolChains/BareMetal.cpp   | 226 
 clang/lib/Driver/ToolChains/BareMetal.h |  34 ++-
 clang/test/Driver/arm-gnutools.c|  12 ++
 clang/test/Driver/baremetal-multilib.yaml   |   4 +-
 clang/test/Driver/baremetal-sysroot.cpp |   4 +-
 clang/test/Driver/baremetal.cpp | 123 ++-
 clang/test/Driver/riscv-args.c  |   2 +-
 clang/test/Driver/riscv32-toolchain-extra.c |   4 +-
 clang/test/Driver/riscv32-toolchain.c   |   6 +-
 clang/test/Driver/riscv64-toolchain-extra.c |   4 +-
 clang/test/Driver/riscv64-toolchain.c   |   4 +-
 12 files changed, 316 insertions(+), 113 deletions(-)
 create mode 100644 clang/test/Driver/arm-gnutools.c

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 7de8341b8d2d61..543ed9e301ef84 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6521,11 +6521,7 @@ const ToolChain &Driver::getToolChain(const ArgList 
&Args,
 break;
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
-if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
-  TC =
-  std::make_unique(*this, Target, 
Args);
-else
-  TC = std::make_unique(*this, Target, Args);
+TC = std::make_unique(*this, Target, Args);
 break;
   case llvm::Triple::ve:
 TC = std::make_unique(*this, Target, Args);
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index f9a73f60973e4c..66a76d56ad8c48 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -97,7 +97,8 @@ static bool findRISCVMultilibs(const Driver &D,
   return false;
 }
 
-static std::string computeBaseSysRoot(const Driver &D, bool IncludeTriple) {
+static std::string computeInstalledToolchainSysRoot(const Driver &D,
+bool IncludeTriple) {
   if (!D.SysRoot.empty())
 return D.SysRoot;
 
@@ -110,20 +111,95 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+// GCC sysroot here means form sysroot from either --gcc-install-dir, or from
+// --gcc-toolchain or if the toolchain is installed alongside clang in
+// bin/../ directory if it is not explicitly specified on the
+// command line through `--sysroot` option. libc here will be newlib.
+std::string BareMetal::computeGCCSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+return getDriver().SysRoot;
+
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain every field.
+llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
+getDriver().getTargetTriple());
+  }
+
+  if (!llvm::sys::fs::exists(SysRootDir))
+return std::string();
+
+  return std::string(SysRootDir);
+}
+
+std::string BareMetal::computeSysRoot() const {
+  if (!SysRoot.empty())
+return SysRoot;
+
+  std::string SysRoot = getDriver().SysRoot;
+  if (!SysRoot.empty() && llvm::sys::fs::exists(SysRoot))
+return SysRoot;
+
+  // Verify the GCC installation from -gcc-install-dir, --gcc-toolchain, or
+  // alongside clang. If valid, form the sysroot. Otherwise, check
+  // lib/clang-runtimes above the driver.
+  SysRoot = computeGCCSysRoot();
+  if (!SysRoot.empty())
+return SysRoot;
+
+  SysRoot =
+  comp

[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-06 Thread Garvit Gupta via cfe-commits


@@ -503,12 +624,22 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
 
-  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
+  if (!D.SysRoot.empty())
+CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
 
+  Args.addAllArgs(CmdArgs, {options::OPT_u});

quic-garvgupt wrote:

Sure, I will be making these changes in the next patchset. However, I wanted to 
clarify if there is any specific reason why, in the RISCVToolchain baremetal 
object, OPT_L and OPT_u are added separately before FilePathLibArgs, and then 
subsequently the rest of the OPT_* options are added. 
https://github.com/llvm/llvm-project/blob/89e919fb0df391da42dbfd48cd8de268335fe672/clang/lib/Driver/ToolChains/RISCVToolchain.cpp#L201

I understand that for gnuld, the order in which flags and object files appear 
on the command line is relevant, unlike for lld. Was this intentional? If so, I 
will maintain that behavior; otherwise, I will add them in a single 
`addAllArgs(...)` function.

https://github.com/llvm/llvm-project/pull/118809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-06 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/121829

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

This is the first PR in the series of 3 PRs for merging and extending Baremetal
toolchain object. The division of the PRs is as follows:
- Teach Baremetal toolchain about GCC installation and make sysroot and 
assembler
  related changes.
- Changes related to linker job and defaults for CXXStdlib and other runtime 
libs.
- Finally removing the call to RISCVToolchain object.

The above division will also ensure that riscv and arm specific tests are not
modified in the same PR.

RFC: 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4

>From 908c2dfa26c5fa7dea14945e4eba411ebe5b3e82 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 13 Dec 2024 05:31:56 -0800
Subject: [PATCH] [RISCV] Teach Barmetal toolchain about GCC installation(1/3)

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

This is the first PR in the series of 3 PRs for merging and extending Baremetal
toolchain object. The division of the PRs is as follows:
- Teach Baremetal toolchain about GCC installation and make sysroot and 
assembler
  related changes.
- Changes related to linker job and defaults for CXXStdlib and other runtime 
libs.
- Finally removing the call to RISCVToolchain object.

The above division will also ensure that riscv and arm specific tests are not
modified in the same PR.

RFC: 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 121 +++---
 clang/lib/Driver/ToolChains/BareMetal.h   |   8 +-
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/libstdc++.a  |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 
 clang/test/Driver/aarch64-toolchain.c |  61 +
 clang/test/Driver/arm-gnutools.c  |  12 ++
 clang/test/Driver/arm-toolchain-extra.c   |  28 
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 +++
 31 files changed, 320 insertions(+), 20 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create m

[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-06 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

* **#121829** https://app.graphite.dev/github/pr/llvm/llvm-project/121829?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/121829?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121821)

2025-01-06 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/121821

This patch introduces the baretmetal toolchain object about GCC Installation. 
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on 
commandline, then sysroot will be formed from there if paths will be valid. 
Otherwise, it will be fallback to as it already existed in the Baremetal 
toolchaibn object.

Additionally, the restriction to always use integrated assembler is removed 
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv 
specific test will be added in the last PR when driver code related to calling 
of RISCVToolchain object will be removed. Currently in this PR, there is no way 
to test riscv target.

This is the first PR in the series of 3 PRs for merging and extending Baremetal 
toolchain object. The division of the PRs is as follows:
- Teach Baremetal toolchain about GCC installation and make sysroot and 
assembler related changes.
- Changes related to linker job and defaults for CXXStdlib and other runtime 
libs.
- Finally removing the call to RISCVToolchain object.

The above division will also ensure that riscv and arm specific tests are not 
modified in the same PR.

RFC: 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

>From f11eb9f6f7c7407f30593360b788bfd970298146 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 13 Dec 2024 05:31:56 -0800
Subject: [PATCH] [RISCV] Teach Barmetal toolchain about GCC installation(1/3)

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

This is the first PR in the series of 3 PRs for merging and extending Baremetal
toolchain object. The division of the PRs is as follows:
- Teach Baremetal toolchain about GCC installation and make sysroot and 
assembler
  related changes.
- Changes related to linker job and defaults for CXXStdlib and other runtime 
libs.
- Finally removing the call to RISCVToolchain object.

The above division will also ensure that riscv and arm specific tests are not
modified in the same PR.

RFC: 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 121 +++---
 clang/lib/Driver/ToolChains/BareMetal.h   |   8 +-
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/libstdc++.a  |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 
 clang/test/Driver/aarch64-toolchain.c |  61 +
 clang/test/Driver/arm-gnutools.c  |  12 ++
 clang/test/Driver/arm-toolchain-extra.c   |  28 
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 +++
 31 files changed, 320 insertions(+), 20 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aar

[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121821)

2025-01-06 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/121821
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-06 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt ready_for_review 
https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-06 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-07 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/121829

>From 02d70a2c1fa746eafa87308f0c8109bb2c91164f Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 13 Dec 2024 05:31:56 -0800
Subject: [PATCH] [RISCV] Teach Barmetal toolchain about GCC installation(1/3)

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

This is the first PR in the series of 3 PRs for merging and extending Baremetal
toolchain object. The division of the PRs is as follows:
- Teach Baremetal toolchain about GCC installation and make sysroot and 
assembler
  related changes.
- Changes related to linker job and defaults for CXXStdlib and other runtime 
libs.
- Finally removing the call to RISCVToolchain object.

The above division will also ensure that riscv and arm specific tests are not
modified in the same PR.

RFC: 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 121 +++---
 clang/lib/Driver/ToolChains/BareMetal.h   |   8 +-
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 
 clang/test/Driver/aarch64-toolchain.c |  61 +
 clang/test/Driver/arm-gnutools.c  |  12 ++
 clang/test/Driver/arm-toolchain-extra.c   |  29 +
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 +++
 30 files changed, 321 insertions(+), 20 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o
 create mode 100644 
cla

[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-02-04 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @petrhosek, following up on our last RISC-V embedded sync-up, can you please 
review this patch? Thanks

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-11 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/118809

>From 569feb86c654be044781aa5968907a72ea882e88 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 13 Nov 2024 02:45:51 -0800
Subject: [PATCH] [WIP] Merging RISCVToolChain and BareMetal toolchains

Currently, LLVM has two RISC-V toolchain classes in Clang for baremetal
development, creating unnecessary maintenance overhead. This patch extends the
BareMetal toolchain to support an existing GCC installation, resolving this 
issue.

The latest patchset preserves the behavior of both toolchain objects with minor
differences. If no --sysroot option is passed on the command line or if the GCC
installation is invalid, the sysroot will first be formed as per the
RISCVToolChain baremetal object. If this path does not exist, the sysroot will
be formed as per the BareMetal toolchain object.

Additionally, the presence of --gcc-toolchain or --gcc-install-dir will imply
that GNU linker is the default linker unless otherwise a differnt linker is
passed through `-fuse-ld` flag.

RFC - 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

change-Id: Ie2cdefd3c95b25770a33319ce2e711c9300efc2e
---
 clang/lib/Driver/Driver.cpp   |   4 -
 clang/lib/Driver/ToolChains/BareMetal.cpp | 247 ++
 clang/lib/Driver/ToolChains/BareMetal.h   |  29 +-
 .../lib/Driver/ToolChains/RISCVToolchain.cpp  | 232 
 clang/lib/Driver/ToolChains/RISCVToolchain.h  |  67 -
 clang/test/Driver/arm-gnutools.c  |  12 +
 clang/test/Driver/baremetal-multilib.yaml |   2 +-
 clang/test/Driver/baremetal-sysroot.cpp   |   2 +-
 clang/test/Driver/baremetal.cpp   |  77 +++---
 clang/test/Driver/riscv-args.c|   2 +-
 clang/test/Driver/riscv32-toolchain-extra.c   |   7 +-
 clang/test/Driver/riscv32-toolchain.c |  22 +-
 clang/test/Driver/riscv64-toolchain-extra.c   |   7 +-
 clang/test/Driver/riscv64-toolchain.c |  16 +-
 14 files changed, 295 insertions(+), 431 deletions(-)
 delete mode 100644 clang/lib/Driver/ToolChains/RISCVToolchain.cpp
 delete mode 100644 clang/lib/Driver/ToolChains/RISCVToolchain.h
 create mode 100644 clang/test/Driver/arm-gnutools.c

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index fb73b62cf2daed..48b6361f588843 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -6557,10 +6557,6 @@ const ToolChain &Driver::getToolChain(const ArgList 
&Args,
 break;
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
-if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
-  TC =
-  std::make_unique(*this, Target, 
Args);
-else
   TC = std::make_unique(*this, Target, Args);
 break;
   case llvm::Triple::ve:
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index eecaaa9a42930d..a2fecb297cd05a 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -97,7 +97,8 @@ static bool findRISCVMultilibs(const Driver &D,
   return false;
 }
 
-static std::string computeBaseSysRoot(const Driver &D, bool IncludeTriple) {
+static std::string computeInstalledToolchainSysRoot(const Driver &D,
+bool IncludeTriple) {
   if (!D.SysRoot.empty())
 return D.SysRoot;
 
@@ -110,20 +111,95 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+// GCC sysroot here means form sysroot from either --gcc-install-dir, or from
+// --gcc-toolchain or if the toolchain is installed alongside clang in
+// bin/../ directory if it is not explicitly specified on the
+// command line through `--sysroot` option. libc here will be newlib.
+std::string BareMetal::computeGCCSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+return getDriver().SysRoot;
+
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain every field.
+llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
+getDriver().getTargetTriple());
+  }
+
+  if (!llvm::sys::fs::exists(SysRootDir))
+return std::string();
+
+  return std::string(SysRootDir);
+}
+
+std::string BareMetal::computeSysRoot() const {
+  if (!SysRoot.empty())
+return SysRoot;
+
+  std::string SysRoot = getDriver().SysRoot;
+  if (!SysRoot.empty() && llvm::sys::fs::exists(SysRoot))
+return SysRoot;
+
+  // Verify the GCC installation from -gcc-install-dir, --gcc-toolchain,

[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-11 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/118809

>From b58f9dd197c78b1e39676022a421ca0e6d5552a5 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 13 Nov 2024 02:45:51 -0800
Subject: [PATCH] [WIP] Merging RISCVToolChain and BareMetal toolchains

Currently, LLVM has two RISC-V toolchain classes in Clang for baremetal
development, creating unnecessary maintenance overhead. This patch extends the
BareMetal toolchain to support an existing GCC installation, resolving this 
issue.

The latest patchset preserves the behavior of both toolchain objects with minor
differences. If no --sysroot option is passed on the command line or if the GCC
installation is invalid, the sysroot will first be formed as per the
RISCVToolChain baremetal object. If this path does not exist, the sysroot will
be formed as per the BareMetal toolchain object.

Additionally, the presence of --gcc-toolchain or --gcc-install-dir will imply
that GNU linker is the default linker unless otherwise a differnt linker is
passed through `-fuse-ld` flag.

RFC - 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

change-Id: Ie2cdefd3c95b25770a33319ce2e711c9300efc2e
---
 clang/lib/Driver/CMakeLists.txt   |   1 -
 clang/lib/Driver/Driver.cpp   |   7 +-
 clang/lib/Driver/ToolChains/BareMetal.cpp | 247 ++
 clang/lib/Driver/ToolChains/BareMetal.h   |  29 +-
 .../lib/Driver/ToolChains/RISCVToolchain.cpp  | 232 
 clang/lib/Driver/ToolChains/RISCVToolchain.h  |  67 -
 clang/test/Driver/arm-gnutools.c  |  12 +
 clang/test/Driver/baremetal-multilib.yaml |   2 +-
 clang/test/Driver/baremetal-sysroot.cpp   |   2 +-
 clang/test/Driver/baremetal.cpp   |  77 +++---
 clang/test/Driver/riscv-args.c|   2 +-
 clang/test/Driver/riscv32-toolchain-extra.c   |   7 +-
 clang/test/Driver/riscv32-toolchain.c |  22 +-
 clang/test/Driver/riscv64-toolchain-extra.c   |   7 +-
 clang/test/Driver/riscv64-toolchain.c |  20 +-
 15 files changed, 298 insertions(+), 436 deletions(-)
 delete mode 100644 clang/lib/Driver/ToolChains/RISCVToolchain.cpp
 delete mode 100644 clang/lib/Driver/ToolChains/RISCVToolchain.h
 create mode 100644 clang/test/Driver/arm-gnutools.c

diff --git a/clang/lib/Driver/CMakeLists.txt b/clang/lib/Driver/CMakeLists.txt
index 4fd10bf671512f..59c219e1f709b4 100644
--- a/clang/lib/Driver/CMakeLists.txt
+++ b/clang/lib/Driver/CMakeLists.txt
@@ -74,7 +74,6 @@ add_clang_library(clangDriver
   ToolChains/OHOS.cpp
   ToolChains/OpenBSD.cpp
   ToolChains/PS4CPU.cpp
-  ToolChains/RISCVToolchain.cpp
   ToolChains/Solaris.cpp
   ToolChains/SPIRV.cpp
   ToolChains/TCE.cpp
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index fb73b62cf2daed..e16b0af1790cc9 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -41,7 +41,6 @@
 #include "ToolChains/PPCFreeBSD.h"
 #include "ToolChains/PPCLinux.h"
 #include "ToolChains/PS4CPU.h"
-#include "ToolChains/RISCVToolchain.h"
 #include "ToolChains/SPIRV.h"
 #include "ToolChains/Solaris.h"
 #include "ToolChains/TCE.h"
@@ -6557,11 +6556,7 @@ const ToolChain &Driver::getToolChain(const ArgList 
&Args,
 break;
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
-if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
-  TC =
-  std::make_unique(*this, Target, 
Args);
-else
-  TC = std::make_unique(*this, Target, Args);
+TC = std::make_unique(*this, Target, Args);
 break;
   case llvm::Triple::ve:
 TC = std::make_unique(*this, Target, Args);
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index eecaaa9a42930d..a2fecb297cd05a 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -97,7 +97,8 @@ static bool findRISCVMultilibs(const Driver &D,
   return false;
 }
 
-static std::string computeBaseSysRoot(const Driver &D, bool IncludeTriple) {
+static std::string computeInstalledToolchainSysRoot(const Driver &D,
+bool IncludeTriple) {
   if (!D.SysRoot.empty())
 return D.SysRoot;
 
@@ -110,20 +111,95 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+// GCC sysroot here means form sysroot from either --gcc-install-dir, or from
+// --gcc-toolchain or if the toolchain is installed alongside clang in
+// bin/../ directory if it is not explicitly specified on the
+// command line through `--sysroot` option. libc here will be newlib.
+std::string BareMetal::computeGCCSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+return getDriver().SysRoot;
+
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getPa

[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-11 Thread Garvit Gupta via cfe-commits


@@ -6521,11 +6521,7 @@ const ToolChain &Driver::getToolChain(const ArgList 
&Args,
 break;
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
-if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
-  TC =
-  std::make_unique(*this, Target, 
Args);
-else
-  TC = std::make_unique(*this, Target, Args);

quic-garvgupt wrote:

yes, I have deleted those files as a part of my current patchset

https://github.com/llvm/llvm-project/pull/118809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-11 Thread Garvit Gupta via cfe-commits


@@ -110,20 +111,95 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+// GCC sysroot here means form sysroot from either --gcc-install-dir, or from
+// --gcc-toolchain or if the toolchain is installed alongside clang in
+// bin/../ directory if it is not explicitly specified on the
+// command line through `--sysroot` option. libc here will be newlib.
+std::string BareMetal::computeGCCSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+return getDriver().SysRoot;
+
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain every field.
+llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
+getDriver().getTargetTriple());
+  }
+
+  if (!llvm::sys::fs::exists(SysRootDir))
+return std::string();
+
+  return std::string(SysRootDir);
+}
+
+std::string BareMetal::computeSysRoot() const {
+  if (!SysRoot.empty())
+return SysRoot;
+
+  std::string SysRoot = getDriver().SysRoot;
+  if (!SysRoot.empty() && llvm::sys::fs::exists(SysRoot))
+return SysRoot;
+
+  // Verify the GCC installation from -gcc-install-dir, --gcc-toolchain, or
+  // alongside clang. If valid, form the sysroot. Otherwise, check
+  // lib/clang-runtimes above the driver.
+  SysRoot = computeGCCSysRoot();
+  if (!SysRoot.empty())
+return SysRoot;
+
+  SysRoot =
+  computeInstalledToolchainSysRoot(getDriver(), /*IncludeTriple*/ true);
+
+  return SysRoot;
+}
+
+static void addMultilibsFilePaths(const Driver &D, const MultilibSet 
&Multilibs,
+  const Multilib &Multilib,
+  StringRef InstallPath,
+  ToolChain::path_list &Paths) {
+  if (const auto &PathsCallback = Multilibs.filePathsCallback())
+for (const auto &Path : PathsCallback(Multilib))
+  addPathIfExists(D, InstallPath + Path, Paths);
+}
+
 BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
  const ArgList &Args)
-: ToolChain(D, Triple, Args),
-  SysRoot(computeBaseSysRoot(D, /*IncludeTriple=*/true)) {
-  getProgramPaths().push_back(getDriver().Dir);
-
-  findMultilibs(D, Triple, Args);
-  SmallString<128> SysRoot(computeSysRoot());
-  if (!SysRoot.empty()) {
-for (const Multilib &M : getOrderedMultilibs()) {
-  SmallString<128> Dir(SysRoot);
-  llvm::sys::path::append(Dir, M.osSuffix(), "lib");
-  getFilePaths().push_back(std::string(Dir));
-  getLibraryPaths().push_back(std::string(Dir));
+: Generic_ELF(D, Triple, Args) {
+  GCCInstallation.init(Triple, Args);
+  SysRoot = computeSysRoot();
+  UseLD =
+  Args.getLastArgValue(options::OPT_fuse_ld_EQ).equals_insensitive("ld");

quic-garvgupt wrote:

value will be "0". The function to decide the default linker is 
"getDefaultLinker" which calls "isUsingLd". I have made this function virtual 
which can be overridden in derived classes and can return false for all cases.

https://github.com/llvm/llvm-project/pull/118809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2024-12-11 Thread Garvit Gupta via cfe-commits


@@ -78,8 +86,9 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
   using OrderedMultilibs =
   
llvm::iterator_range::const_reverse_iterator>;
   OrderedMultilibs getOrderedMultilibs() const;
-
+  bool UseLD;

quic-garvgupt wrote:

please see my comment below

https://github.com/llvm/llvm-project/pull/118809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-20 Thread Garvit Gupta via cfe-commits


@@ -110,20 +111,93 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+// GCC sysroot here means form sysroot from either --gcc-install-dir, or from
+// --gcc-toolchain or if the toolchain is installed alongside clang in
+// bin/../ directory if it is not explicitly specified on the
+// command line through `--sysroot` option. libc here will be newlib.
+std::string BareMetal::computeGCCSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+return getDriver().SysRoot;
+
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain every field.
+llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
+getDriver().getTargetTriple());
+  }
+
+  if (!llvm::sys::fs::exists(SysRootDir))

quic-garvgupt wrote:

 I understand that. However, to preserve the behavior of both the BareMetal and 
RISCVToolchain objects, I need to maintain this condition. If this returns 
empty, only then does the control transfer to compute the sysroot, as it was 
done previously in the BareMetal toolchain. (See line 152 under 
`BareMetal::computeSysRoot()` function)

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-29 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Gentle Ping again!

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-13 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Gentle Ping!

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-10 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2025-01-09 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

I have divided this PR into three separate PRs to streamline the review process 
and ensured that ARM and RISCV tests are not modified in the same PR. Please 
review them and provide any feedback or comments. Thanks!

[RISCV] Teach Barmetal toolchain about GCC installation(1/3) - 
https://github.com/llvm/llvm-project/pull/121831
[RISCV] Change linker job in Baremetal toolchain object accomodate 
GCCInstallation.(2/3) - https://github.com/llvm/llvm-project/pull/121830
[RISCV] Integrate RISCV target in baremetal toolchain object and deprecate 
RISCVToolchain object.(3/3) - https://github.com/llvm/llvm-project/pull/121829

https://github.com/llvm/llvm-project/pull/118809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-01-07 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/121829

>From 03b91a8d408bacec17391b995611dbbb0ea62edd Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 13 Dec 2024 05:31:56 -0800
Subject: [PATCH] [RISCV] Teach Barmetal toolchain about GCC installation(1/3)

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

This is the first PR in the series of 3 PRs for merging and extending Baremetal
toolchain object. The division of the PRs is as follows:
- Teach Baremetal toolchain about GCC installation and make sysroot and 
assembler
  related changes.
- Changes related to linker job and defaults for CXXStdlib and other runtime 
libs.
- Finally removing the call to RISCVToolchain object.

The above division will also ensure that riscv and arm specific tests are not
modified in the same PR.

RFC: 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 121 +++---
 clang/lib/Driver/ToolChains/BareMetal.h   |  13 +-
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/bin/ld   |   1 +
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/bin/ld   |   1 +
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 
 clang/test/Driver/aarch64-toolchain.c |  61 +
 clang/test/Driver/arm-gnutools.c  |  12 ++
 clang/test/Driver/arm-toolchain-extra.c   |  29 +
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 +++
 30 files changed, 326 insertions(+), 20 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o
 create mode 100644 
cla

[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-03-24 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/121829

>From e07a4cd4e0ff77f74b66695923bc998904c14746 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 13 Dec 2024 05:31:56 -0800
Subject: [PATCH] [Driver] Teach Barmetal toolchain about GCC installation

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object. Moreover, support for adding include paths for libstd
C++ library is added as well.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

RFC:
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 130 ++
 clang/lib/Driver/ToolChains/BareMetal.h   |  14 +-
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 
 clang/test/Driver/aarch64-toolchain.c |  61 
 clang/test/Driver/arm-gnutools.c  |  12 ++
 clang/test/Driver/arm-toolchain-extra.c   |  29 
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 +++
 28 files changed, 324 insertions(+), 32 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100755 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/bin/aarch64-none-elf-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o
 create mode 100755 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/bin/aarch64-none-elf-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100755 
clang/test/Driver/Inputs/basic_arm_gcc_tree/bin/armv6m-none-eabi-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crtend.o
 create mode 100755 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/bin/armv6m-none-eabi-ld
 create mode 100644 clang/test/Driver/aarch64-toolchain-extra.c
 create mode 100644 clang/test/Driver/aarch64-toolchain.c
 create mode 100644 clan

[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-03-24 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-03-24 Thread Garvit Gupta via cfe-commits


@@ -110,20 +110,76 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+std::string BareMetal::computeSysRoot() const {
+  if (!SysRoot.empty())
+return SysRoot;
+
+  std::string SysRoot = getDriver().SysRoot;
+  if (!SysRoot.empty())
+return SysRoot;
+
+  // Verify the GCC installation from -gcc-install-dir, --gcc-toolchain, or
+  // alongside clang. If valid, form the sysroot. Otherwise, check
+  // lib/clang-runtimes above the driver.
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain every field.
+llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
+getDriver().getTargetTriple());
+  }
+
+  if (llvm::sys::fs::exists(SysRootDir))
+return std::string(SysRootDir);
+  SysRoot = computeBaseSysRoot(getDriver(), /*IncludeTriple*/ true);
+
+  return SysRoot;

quic-garvgupt wrote:

Thanks for pointing this out!. I have fixed it in the next patchset

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-03-24 Thread Garvit Gupta via cfe-commits


@@ -79,7 +87,6 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
   OrderedMultilibs getOrderedMultilibs() const;
 
   std::string SysRoot;

quic-garvgupt wrote:

Removing this private variable was making the code messy because there have 
been updates to this variable in some member functions and the updated value is 
then further used in other functions.  Removing this variable would need to 
pass this as an argument to all those functions which would make the code less 
readable.

To avoid the confusion, all local variables for sysroot are named as 
`SysRootDir` and any references to this variable as `SysRoot` only

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-04-23 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

> I'm looking at this patch in isolation, so its possible that the comments 
> here are addressed elasewhere.
> 
> It looks like useful functionality to add to the bare metal driver.
> 
> It also looks like there is some scope for some documentation, for users and 
> for toolchain developers. Users will need to know about what options to set 
> to point at their GCC installation (or place adjacent). Non-GNU toolchain 
> developers will need to avoid `/lib/crt0.o`. If multilibs doesn't 
> work for all toolchains yet, would be good to add that as a limitation.
> 
> That could be done with a follow up patch though.

Thanks for providing feedback @smithp35. I can add documentation related to the 
points you mentioned. Pls let me know if adding them in the BareMetal.cpp file 
would be ok or should I consider adding it in some other file.

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-04-23 Thread Garvit Gupta via cfe-commits


@@ -110,20 +110,81 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+static bool hasGCCToolChainAlongSideClang(const Driver &D) {
+  SmallString<128> GCCDir;
+  llvm::sys::path::append(GCCDir, D.Dir, "..", D.getTargetTriple(),
+  "lib/crt0.o");
+  return llvm::sys::fs::exists(GCCDir);
+}
+
+std::string BareMetal::computeSysRoot() const {
+  if (!SysRoot.empty())
+return SysRoot;
+
+  const Driver &D = getDriver();
+  if (!D.SysRoot.empty())
+return D.SysRoot;
+
+  // Verify the GCC installation from -gcc-install-dir, --gcc-toolchain, or
+  // alongside clang. If valid, form the sysroot. Otherwise, check
+  // lib/clang-runtimes above the driver.
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else if (hasGCCToolChainAlongSideClang(D)) {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain every field.
+llvm::sys::path::append(SysRootDir, D.Dir, "..", D.getTargetTriple());
+  }
+
+  if (llvm::sys::fs::exists(SysRootDir))
+return std::string(SysRootDir);
+  return computeBaseSysRoot(D, /*IncludeTriple*/ true);
+}
+
+static void addMultilibsFilePaths(const Driver &D, const MultilibSet 
&Multilibs,
+  const Multilib &Multilib,
+  StringRef InstallPath,
+  ToolChain::path_list &Paths) {
+  if (const auto &PathsCallback = Multilibs.filePathsCallback())
+for (const auto &Path : PathsCallback(Multilib))
+  addPathIfExists(D, InstallPath + Path, Paths);
+}
+
 BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
  const ArgList &Args)
-: ToolChain(D, Triple, Args),
-  SysRoot(computeBaseSysRoot(D, /*IncludeTriple=*/true)) {
-  getProgramPaths().push_back(getDriver().Dir);
-
-  findMultilibs(D, Triple, Args);
-  SmallString<128> SysRoot(computeSysRoot());
-  if (!SysRoot.empty()) {
-for (const Multilib &M : getOrderedMultilibs()) {
-  SmallString<128> Dir(SysRoot);
-  llvm::sys::path::append(Dir, M.osSuffix(), "lib");
-  getFilePaths().push_back(std::string(Dir));
-  getLibraryPaths().push_back(std::string(Dir));
+: Generic_ELF(D, Triple, Args) {
+  GCCInstallation.init(Triple, Args);
+  SysRoot = computeSysRoot();
+  if (GCCInstallation.isValid()) {
+Multilibs = GCCInstallation.getMultilibs();
+SelectedMultilibs.assign({GCCInstallation.getMultilib()});

quic-garvgupt wrote:

Yes, GNU arm-none-eabi toolchains won't work since mutlilibs for this target is 
not added. Currently, only form some targets hard-coded multilibs are added 
including RISCV. One can look at `ScanGCCForMultilibs` function in `Gnu.cpp` to 
see for what all architectures GCC multilibs are supported IINW.

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-03-11 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @petrhosek, I've addressed all your comments. Please review the changes and 
approve the PR if everything looks good.

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-03-11 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/121829

>From 9bb78235d2ba008dc49dc4746edbd9978a92396b Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 13 Dec 2024 05:31:56 -0800
Subject: [PATCH] [RISCV] Teach Barmetal toolchain about GCC installation(1/3)

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

This is the first PR in the series of 3 PRs for merging and extending Baremetal
toolchain object. The division of the PRs is as follows:
- Teach Baremetal toolchain about GCC installation and make sysroot and 
assembler
  related changes.
- Changes related to linker job and defaults for CXXStdlib and other runtime 
libs.
- Finally removing the call to RISCVToolchain object.

The above division will also ensure that riscv and arm specific tests are not
modified in the same PR.

RFC: 
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 97 ---
 clang/lib/Driver/ToolChains/BareMetal.h   | 13 ++-
 .../aarch64-none-elf/bin/ld   |  1 +
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |  0
 .../aarch64-none-elf/lib/.keep|  0
 .../aarch64-none-elf/lib/crt0.o   |  0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |  0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |  0
 .../aarch64-none-elf/bin/ld   |  1 +
 .../aarch64-none-elf/lib/.keep|  0
 .../aarch64-none-elf/lib/crt0.o   |  0
 .../aarch64-none-elf/lib/crtbegin.o   |  0
 .../aarch64-none-elf/lib/crtend.o |  0
 .../armv6m-none-eabi/bin/ld   |  1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |  0
 .../armv6m-none-eabi/lib/.keep|  0
 .../armv6m-none-eabi/lib/crt0.o   |  0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |  0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |  0
 .../armv6m-none-eabi/bin/ld   |  1 +
 .../armv6m-none-eabi/lib/.keep|  0
 .../armv6m-none-eabi/lib/crt0.o   |  0
 .../armv6m-none-eabi/lib/crtbegin.o   |  0
 .../armv6m-none-eabi/lib/crtend.o |  0
 clang/test/Driver/aarch64-toolchain-extra.c   | 28 ++
 clang/test/Driver/aarch64-toolchain.c | 61 
 clang/test/Driver/arm-gnutools.c  | 12 +++
 clang/test/Driver/arm-toolchain-extra.c   | 29 ++
 clang/test/Driver/arm-toolchain.c | 62 
 clang/test/Driver/baremetal.cpp   | 16 +++
 30 files changed, 304 insertions(+), 18 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/bin/ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inp

[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-02-23 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

It's been a few weeks since this patch was last reviewed. If everything looks 
good, could someone please provide an LGTM? I'd like to merge this patch soon.



https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation(1/3) (PR #121829)

2025-02-23 Thread Garvit Gupta via cfe-commits


@@ -110,20 +111,93 @@ static std::string computeBaseSysRoot(const Driver &D, 
bool IncludeTriple) {
   return std::string(SysRootDir);
 }
 
+// GCC sysroot here means form sysroot from either --gcc-install-dir, or from
+// --gcc-toolchain or if the toolchain is installed alongside clang in
+// bin/../ directory if it is not explicitly specified on the
+// command line through `--sysroot` option. libc here will be newlib.
+std::string BareMetal::computeGCCSysRoot() const {
+  if (!getDriver().SysRoot.empty())
+return getDriver().SysRoot;
+
+  SmallString<128> SysRootDir;
+  if (GCCInstallation.isValid()) {
+StringRef LibDir = GCCInstallation.getParentLibPath();
+StringRef TripleStr = GCCInstallation.getTriple().str();
+llvm::sys::path::append(SysRootDir, LibDir, "..", TripleStr);
+  } else {
+// Use the triple as provided to the driver. Unlike the parsed triple
+// this has not been normalized to always contain every field.
+llvm::sys::path::append(SysRootDir, getDriver().Dir, "..",
+getDriver().getTargetTriple());
+  }
+
+  if (!llvm::sys::fs::exists(SysRootDir))

quic-garvgupt wrote:

I agree that the condition can be removed if it is actually not needed. Though 
that has to be done as a part of new patch because for now, this patch aims to 
preserve the behavior as it was before. 

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-04-04 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-04-03 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @petrhosek, I've addressed all your comments. Please review the changes and 
approve the PR if everything looks good.

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-04-04 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/121829

>From 1d0db96afab52903a267d600cab02c24900101af Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 13 Dec 2024 05:31:56 -0800
Subject: [PATCH] [Driver] Teach Barmetal toolchain about GCC installation

This patch introduces the baretmetal toolchain object about GCC Installation.
Currently, if `--gcc-installation` ot `--gcc-install-dir` options are passed on
commandline, then sysroot will be formed from there if paths will be valid.
Otherwise, it will be fallback to as it already existed in the Baremetal
toolchaibn object. Moreover, support for adding include paths for libstd
C++ library is added as well.

Additionally, the restriction to always use integrated assembler is removed
because with valid gcc installation, gnu assembler can be invoked as well.

This patch currently adds and modifies arm related test only. The riscv specific
test will be added in the last PR when driver code related to calling of
RISCVToolchain object will be removed. Currently in this PR, there is no way to
test riscv target.

RFC:
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ibaeb569cf7e2cee03c022aa9ecd1abe29d5c30d4
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 130 ++
 clang/lib/Driver/ToolChains/BareMetal.h   |  14 +-
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 
 clang/test/Driver/aarch64-toolchain.c |  61 
 clang/test/Driver/arm-gnutools.c  |  12 ++
 clang/test/Driver/arm-toolchain-extra.c   |  29 
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 +++
 28 files changed, 324 insertions(+), 32 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100755 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/bin/aarch64-none-elf-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o
 create mode 100755 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/bin/aarch64-none-elf-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100755 
clang/test/Driver/Inputs/basic_arm_gcc_tree/bin/armv6m-none-eabi-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crtend.o
 create mode 100755 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/bin/armv6m-none-eabi-ld
 create mode 100644 clang/test/Driver/aarch64-toolchain-extra.c
 create mode 100644 clang/test/Driver/aarch64-toolchain.c
 create mode 100644 clan

[clang] Reland [Driver] Add support for crtbegin.o, crtend.o and libgloss lib to BareMetal toolchain object (PR #144649)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/144649

>From 14a2bb5dead7fa0e3d442e04ba73ad5f98d2e90e Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 17 Jun 2025 22:49:55 +0530
Subject: [PATCH] [Driver] Add support for crtbegin.o, crtend.o and libgloss
 lib to BareMetal toolchain object (#121830)

This patch conditionalise the addition of crt{begin,end}.o object files
along
with addition of -lgloss lib based on whether libc selected is newlib or
llvm
libc. Since there is no way a user can specify which libc it wants to
link
against, currently passing valid GCCInstallation to driver will select
newlib
otherwise it will default to llvm libc.

Moreover, this patch makes gnuld the default linker for baremetal
toolchain
object. User need to pass `-fuse-ld=lld` explicitly to driver to select
lld

This is the 2nd patch in the series of patches of merging RISCVToolchain
into
BareMetal toolchain object.

RFC:

https: 
//discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
Change-Id: Ie8eca262b53550d70c40c02f3395b2958ef19351
---
 clang/lib/Driver/ToolChains/BareMetal.cpp   | 37 +++-
 clang/lib/Driver/ToolChains/BareMetal.h |  3 +-
 clang/test/Driver/aarch64-toolchain-extra.c | 13 ++-
 clang/test/Driver/aarch64-toolchain.c   | 95 
 clang/test/Driver/arm-toolchain-extra.c |  7 ++
 clang/test/Driver/arm-toolchain.c   | 99 -
 clang/test/Driver/baremetal.cpp |  3 +-
 clang/test/Driver/sanitizer-ld.c|  2 +-
 8 files changed, 246 insertions(+), 13 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index 0fbfe6c77f342..a08bb588dd764 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -584,9 +584,31 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 CmdArgs.push_back(Arch == llvm::Triple::aarch64_be ? "-EB" : "-EL");
   }
 
-  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
-   options::OPT_r)) {
-CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
+  bool NeedCRTs =
+  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
+
+  const char *CRTBegin, *CRTEnd;
+  if (NeedCRTs) {
+if (!Args.hasArg(options::OPT_r))
+  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
+if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
+  auto RuntimeLib = TC.GetRuntimeLibType(Args);
+  switch (RuntimeLib) {
+  case (ToolChain::RLT_Libgcc): {
+CRTBegin = "crtbegin.o";
+CRTEnd = "crtend.o";
+break;
+  }
+  case (ToolChain::RLT_CompilerRT): {
+CRTBegin =
+TC.getCompilerRTArgString(Args, "crtbegin", ToolChain::FT_Object);
+CRTEnd =
+TC.getCompilerRTArgString(Args, "crtend", ToolChain::FT_Object);
+break;
+  }
+  }
+  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(CRTBegin)));
+}
   }
 
   Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
@@ -609,15 +631,22 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   }
 
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
+CmdArgs.push_back("--start-group");
 AddRunTimeLibs(TC, D, CmdArgs, Args);
-
 CmdArgs.push_back("-lc");
+if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D))
+  CmdArgs.push_back("-lgloss");
+CmdArgs.push_back("--end-group");
   }
 
   if (D.isUsingLTO())
 addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
   D.getLTOMode() == LTOK_Thin);
 
+  if ((TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) &&
+  NeedCRTs)
+CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(CRTEnd)));
+
   if (TC.getTriple().isRISCV())
 CmdArgs.push_back("-X");
 
diff --git a/clang/lib/Driver/ToolChains/BareMetal.h 
b/clang/lib/Driver/ToolChains/BareMetal.h
index 930f8584e6435..54805530bae82 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.h
+++ b/clang/lib/Driver/ToolChains/BareMetal.h
@@ -38,6 +38,7 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public Generic_ELF {
 public:
   bool initGCCInstallation(const llvm::Triple &Triple,
const llvm::opt::ArgList &Args);
+  bool hasValidGCCInstallation() const { return IsGCCInstallationValid; }
   bool isBareMetal() const override { return true; }
   bool isCrossCompiling() const override { return true; }
   bool HasNativeLLVMSupport() const override { return true; }
@@ -63,8 +64,6 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public Generic_ELF {
 return ToolChain::CST_Libcxx;
   }
 
-  const char *getDefaultLinker() const override { return "ld.lld"; }
-
   void
   AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
 llvm::opt::ArgStringLis

[clang] [RISCV] Merging RISCVToolChain and BareMetal toolchains (PR #118809)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/118809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland [Driver] Add support for GCC installation detection in Baremetal toolchain (PR #145390)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/145390
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland [Driver] Fix link order of BareMetal toolchain object (PR #146291)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/146291

The linker job in BareMetal toolchain object will be used by GNU ld and lld 
both.
However, gnuld process the arguments in the order in which they appear on 
command
line, whereas there is no such restriction with lld.

The previous order was:
LibraryPaths -> Libraries -> LTOOptions -> LinkerInputs The new order is:
LibraryPaths -> LTOOptions -> LinkerInputs -> Libraries

LTO options need to be added before adding any linker inputs because file format
after compile stage during LTO is bitcode which gnuld natively cannot process.
Hence will need to pass appropriate plugins before adding any bitcode file on 
the
command line.

Object files that are getting linked need to be passed before processing any
libraries so that gnuld can appropriately do symbol resolution for the symbols
for which no definition is provided through user code.

Similar link order is also followed by other linker jobs for gnuld such as in
gnutools::Linker in Gnu.cpp

This is the 3rd patch in the series of patches of merging RISCVToolchain into
BareMetal toolchain object.

RFC:
https: 
//discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

>From 246f30222eafef9e363c884ddc8744ecc9c4d02f Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 17 Jun 2025 23:43:07 +0530
Subject: [PATCH] Reland  [Driver] Fix link order of BareMetal toolchain object
 (#132806)

The linker job in BareMetal toolchain object will be used by GNU ld and
lld both.
However, gnuld process the arguments in the order in which they appear
on command
line, whereas there is no such restriction with lld.

The previous order was:
LibraryPaths -> Libraries -> LTOOptions -> LinkerInputs
The new order is:
LibraryPaths -> LTOOptions -> LinkerInputs -> Libraries

LTO options need to be added before adding any linker inputs because
file format
after compile stage during LTO is bitcode which gnuld natively cannot
process.
Hence will need to pass appropriate plugins before adding any bitcode
file on the
command line.

Object files that are getting linked need to be passed before processing
any
libraries so that gnuld can appropriately do symbol resolution for the
symbols
for which no definition is provided through user code.

Similar link order is also followed by other linker jobs for gnuld such
as in
gnutools::Linker in Gnu.cpp

This is the 3rd patch in the series of patches of merging RISCVToolchain
into
BareMetal toolchain object.

RFC:

https: 
//discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
Change-Id: I36848d7858595c88127ef081a16c663e90f65f17
---
 clang/lib/Driver/ToolChains/BareMetal.cpp   | 12 ++--
 clang/test/Driver/aarch64-toolchain-extra.c |  2 +-
 clang/test/Driver/aarch64-toolchain.c   | 28 
 clang/test/Driver/arm-toolchain-extra.c |  2 +-
 clang/test/Driver/arm-toolchain.c   | 28 
 clang/test/Driver/baremetal-multilib.yaml   |  3 +-
 clang/test/Driver/baremetal-sysroot.cpp |  8 ++-
 clang/test/Driver/baremetal.cpp | 79 +
 8 files changed, 92 insertions(+), 70 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index a08bb588dd764..a665040662a3f 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -568,8 +568,6 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
 
-  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
-
   CmdArgs.push_back("-Bstatic");
 
   if (TC.getTriple().isRISCV() && Args.hasArg(options::OPT_mno_relax))
@@ -619,6 +617,12 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   for (const auto &LibPath : TC.getLibraryPaths())
 CmdArgs.push_back(Args.MakeArgString(llvm::Twine("-L", LibPath)));
 
+  if (D.isUsingLTO())
+addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
+  D.getLTOMode() == LTOK_Thin);
+
+  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
+
   if (TC.ShouldLinkCXXStdlib(Args)) {
 bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) &&
!Args.hasArg(options::OPT_static);
@@ -639,10 +643,6 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 CmdArgs.push_back("--end-group");
   }
 
-  if (D.isUsingLTO())
-addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
-  D.getLTOMode() == LTOK_Thin);
-
   if ((TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) &&
   NeedCRTs)
 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(CRTEnd)));
diff --git a/clang/test/Driver/aarch64-toolchain-extra.c 
b/clang/test/Driver/aarch64-toolchain-extra.c
index 4b22e35314436..4945a622969c6 100644
--- a/clang/test/Driver/aarch64-toolchai

[clang] Reland [Driver] Add support for crtbegin.o, crtend.o and libgloss lib to BareMetal toolchain object (PR #144649)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/144649
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Propogate linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-08 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/147589

>From 6f96fc844751a95ee18b69ddb850ddd2fe76a89c Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 8 Jul 2025 07:21:37 -0700
Subject: [PATCH 1/2] Propogate appropriate linker flags for `-static-pie` in
 BareMetal toolchain

Change-Id: I2ad7cd9d2677334de21ae8abade874ca7b7106b5
---
 clang/include/clang/Driver/CommonArgs.h|  2 ++
 clang/lib/Driver/ToolChains/BareMetal.cpp  | 19 
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 12 ++
 clang/lib/Driver/ToolChains/Gnu.cpp| 12 --
 clang/test/Driver/baremetal.cpp| 26 ++
 5 files changed, 55 insertions(+), 16 deletions(-)

diff --git a/clang/include/clang/Driver/CommonArgs.h 
b/clang/include/clang/Driver/CommonArgs.h
index 26aa3ccf84786..d8877903c892f 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -85,6 +85,8 @@ const char *RelocationModelName(llvm::Reloc::Model Model);
 std::tuple
 ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);
 
+bool getStaticPIE(const llvm::opt::ArgList &Args, const ToolChain &TC);
+
 unsigned ParseFunctionAlignment(const ToolChain &TC,
 const llvm::opt::ArgList &Args);
 
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index e670696cd59ae..6ee3fbe400566 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -599,11 +599,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   const Driver &D = getToolChain().getDriver();
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
+  const bool IsStaticPIE = getStaticPIE(Args, TC);
 
   if (!D.SysRoot.empty())
 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
 
   CmdArgs.push_back("-Bstatic");
+  if(IsStaticPIE) {
+CmdArgs.push_back("-pie");
+CmdArgs.push_back("--no-dynamic-linker");
+CmdArgs.push_back("-z");
+CmdArgs.push_back("text");
+  }
 
   if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
 CmdArgs.push_back("-m");
@@ -633,14 +640,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 
   const char *CRTBegin, *CRTEnd;
   if (NeedCRTs) {
-if (!Args.hasArg(options::OPT_r))
-  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
+if (!Args.hasArg(options::OPT_r)) {
+  const char *crt = "crt0.o";
+  if (IsStaticPIE)
+crt = "rcrt1.o";
+  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(crt)));
+}
 if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
   auto RuntimeLib = TC.GetRuntimeLibType(Args);
   switch (RuntimeLib) {
   case (ToolChain::RLT_Libgcc): {
-CRTBegin = "crtbegin.o";
-CRTEnd = "crtend.o";
+CRTBegin = IsStaticPIE ? "crtbeginS.o" : "crtbegin.o";
+CRTEnd = IsStaticPIE ? "crtendS.o" : "crtend.o";
 break;
   }
   case (ToolChain::RLT_CompilerRT): {
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index bdd77ac84913c..9e16cba0c7e25 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2089,6 +2089,18 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const 
ArgList &Args) {
   return std::make_tuple(RelocM, 0U, false);
 }
 
+bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
+  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
+  if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
+const Driver &D = TC.getDriver();
+const llvm::opt::OptTable &Opts = D.getOpts();
+StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
+StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
+D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
+  }
+  return HasStaticPIE;
+}
+
 // `-falign-functions` indicates that the functions should be aligned to the
 // backend's preferred alignment.
 //
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index f5e2655857432..01b146db24f3e 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -219,18 +219,6 @@ void tools::gcc::Linker::RenderExtraToolArgs(const 
JobAction &JA,
   // The types are (hopefully) good enough.
 }
 
-static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
-  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
-  if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
-const Driver &D = TC.getDriver();
-const llvm::opt::OptTable &Opts = D.getOpts();
-StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
-StringRef NoPIEName = Opts.getOptionName(opti

[clang] [Driver][RISCV] Fix incorrect compiler-rt path override in BareMetal toolchain after RISCVToolChain removal (PR #146849)

2025-07-07 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/146849

>From 3d0220ecce368a481981c414f789d9dbbfc49c81 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Thu, 3 Jul 2025 02:56:01 -0700
Subject: [PATCH] [Driver][RISCV] Fix and print appropriate compiler-rt path
 when GCCInstallation is valid

RISCVToolChain which recently got removed in commit f8cb798, used a
different compiler-rt path relative to resource-dir. However,
this behavior got override when it was merged in BareMetal toolchain.

This patch fixes that.

Without valid GCCInstallation:
//lib/baremetal/libclang_rt.builtins-.a

With valid GCCInstalltion:
//lib/libclang_rt.builtins.a

Change-Id: I37670b24019e3d473139ad6df8c3e6b92196fa6d
---
 clang/lib/Driver/ToolChains/BareMetal.cpp  | 10 ++
 clang/lib/Driver/ToolChains/BareMetal.h|  1 +
 clang/test/Driver/print-libgcc-file-name-clangrt.c |  9 +
 3 files changed, 20 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index 981395deb9dbc..e670696cd59ae 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -206,6 +206,16 @@ std::string BareMetal::computeSysRoot() const {
   return computeClangRuntimesSysRoot(D, /*IncludeTriple*/ true);
 }
 
+std::string BareMetal::getCompilerRTPath() const {
+  const Driver &D = getDriver();
+  if (IsGCCInstallationValid || detectGCCToolchainAdjacent(getDriver())) {
+SmallString<128> Path(D.ResourceDir);
+llvm::sys::path::append(Path, "lib");
+return std::string(Path.str());
+  }
+  return ToolChain::getCompilerRTPath();
+}
+
 static void addMultilibsFilePaths(const Driver &D, const MultilibSet 
&Multilibs,
   const Multilib &Multilib,
   StringRef InstallPath,
diff --git a/clang/lib/Driver/ToolChains/BareMetal.h 
b/clang/lib/Driver/ToolChains/BareMetal.h
index cc57fa21867a2..d3d415b337a0b 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.h
+++ b/clang/lib/Driver/ToolChains/BareMetal.h
@@ -76,6 +76,7 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public Generic_ELF {
   addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;
   std::string computeSysRoot() const override;
+  std::string getCompilerRTPath() const override;
   SanitizerMask getSupportedSanitizers() const override;
 
   SmallVector
diff --git a/clang/test/Driver/print-libgcc-file-name-clangrt.c 
b/clang/test/Driver/print-libgcc-file-name-clangrt.c
index a902eedc85209..1cb28d43748ec 100644
--- a/clang/test/Driver/print-libgcc-file-name-clangrt.c
+++ b/clang/test/Driver/print-libgcc-file-name-clangrt.c
@@ -63,3 +63,12 @@
 // RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-ARM-BAREMETAL-PER-TARGET %s
 // CHECK-CLANGRT-ARM-BAREMETAL-PER-TARGET: libclang_rt.builtins.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=riscv32-unknown-elf \
+// RUN: --sysroot=%S/Inputs/resource_dir_with_arch_subdir \
+// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
+// RUN: --gcc-toolchain=%S/Inputs/basic_riscv32_tree 2>&1 \
+// RUN:| FileCheck --check-prefix=CHECK-CLANGRT-RISCV-BAREMETAL %s
+// CHECK-CLANGRT-RISCV-BAREMETAL-NOT: 
baremetal{{/|\\}}libclang_rt.builtins-riscv32.a
+// CHECK-CLANGRT-RISCV-BAREMETAL: libclang_rt.builtins.a

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


[clang] [Driver][RISCV] Fix incorrect compiler-rt path override in BareMetal toolchain after RISCVToolChain removal (PR #146849)

2025-07-07 Thread Garvit Gupta via cfe-commits


@@ -206,6 +206,15 @@ std::string BareMetal::computeSysRoot() const {
   return computeClangRuntimesSysRoot(D, /*IncludeTriple*/ true);
 }
 
+std::string BareMetal::getCompilerRTPath() const {
+  if (IsGCCInstallationValid || detectGCCToolchainAdjacent(getDriver())) {
+SmallString<128> Path(getDriver().ResourceDir);

quic-garvgupt wrote:

Done

https://github.com/llvm/llvm-project/pull/146849
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver][RISCV] Fix incorrect compiler-rt path override in BareMetal toolchain after RISCVToolChain removal (PR #146849)

2025-07-07 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/146849
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Propogate linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-08 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/147589

>From c6420bc64c2745c49400408ba93afa34eea194e3 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 8 Jul 2025 12:44:44 -0700
Subject: [PATCH] Propogate linker flags when `-static-pie` is enabled in
 BareMetal toolchain

Change-Id: I580875585e9eac2e9568e84650265f71d028f3ff
---
 clang/include/clang/Driver/CommonArgs.h|  2 ++
 clang/lib/Driver/ToolChains/BareMetal.cpp  | 19 +++
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 12 
 clang/lib/Driver/ToolChains/Gnu.cpp| 12 
 clang/test/Driver/aarch64-toolchain.c  | 12 
 clang/test/Driver/arm-toolchain.c  | 12 
 clang/test/Driver/riscv32-toolchain.c  | 13 +
 clang/test/Driver/riscv64-toolchain.c  | 13 +
 8 files changed, 79 insertions(+), 16 deletions(-)

diff --git a/clang/include/clang/Driver/CommonArgs.h 
b/clang/include/clang/Driver/CommonArgs.h
index 26aa3ccf84786..d8877903c892f 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -85,6 +85,8 @@ const char *RelocationModelName(llvm::Reloc::Model Model);
 std::tuple
 ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);
 
+bool getStaticPIE(const llvm::opt::ArgList &Args, const ToolChain &TC);
+
 unsigned ParseFunctionAlignment(const ToolChain &TC,
 const llvm::opt::ArgList &Args);
 
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index e670696cd59ae..73adeaa3dbaf3 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -599,11 +599,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   const Driver &D = getToolChain().getDriver();
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
+  const bool IsStaticPIE = getStaticPIE(Args, TC);
 
   if (!D.SysRoot.empty())
 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
 
   CmdArgs.push_back("-Bstatic");
+  if (IsStaticPIE) {
+CmdArgs.push_back("-pie");
+CmdArgs.push_back("--no-dynamic-linker");
+CmdArgs.push_back("-z");
+CmdArgs.push_back("text");
+  }
 
   if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
 CmdArgs.push_back("-m");
@@ -633,14 +640,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 
   const char *CRTBegin, *CRTEnd;
   if (NeedCRTs) {
-if (!Args.hasArg(options::OPT_r))
-  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
+if (!Args.hasArg(options::OPT_r)) {
+  const char *crt = "crt0.o";
+  if (IsStaticPIE)
+crt = "rcrt1.o";
+  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(crt)));
+}
 if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
   auto RuntimeLib = TC.GetRuntimeLibType(Args);
   switch (RuntimeLib) {
   case (ToolChain::RLT_Libgcc): {
-CRTBegin = "crtbegin.o";
-CRTEnd = "crtend.o";
+CRTBegin = IsStaticPIE ? "crtbeginS.o" : "crtbegin.o";
+CRTEnd = IsStaticPIE ? "crtendS.o" : "crtend.o";
 break;
   }
   case (ToolChain::RLT_CompilerRT): {
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index bdd77ac84913c..f8f97b02a5f95 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2089,6 +2089,18 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const 
ArgList &Args) {
   return std::make_tuple(RelocM, 0U, false);
 }
 
+bool tools::getStaticPIE(const ArgList &Args, const ToolChain &TC) {
+  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
+  if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
+const Driver &D = TC.getDriver();
+const llvm::opt::OptTable &Opts = D.getOpts();
+StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
+StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
+D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
+  }
+  return HasStaticPIE;
+}
+
 // `-falign-functions` indicates that the functions should be aligned to the
 // backend's preferred alignment.
 //
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index f5e2655857432..01b146db24f3e 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -219,18 +219,6 @@ void tools::gcc::Linker::RenderExtraToolArgs(const 
JobAction &JA,
   // The types are (hopefully) good enough.
 }
 
-static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
-  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
-  if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
-const Driver &D = TC.ge

[clang] Add necessary linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-08 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/147589
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Propogate linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-08 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/147589

>From a1f3797d0acb93e66959c5163aeb49213d2d32a1 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 8 Jul 2025 12:44:44 -0700
Subject: [PATCH] Propogate linker flags when `-static-pie` is enabled in
 BareMetal toolchain

Change-Id: I580875585e9eac2e9568e84650265f71d028f3ff
---
 clang/lib/Driver/ToolChains/BareMetal.cpp  |  6 ++
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 12 
 clang/test/Driver/aarch64-toolchain.c  | 12 
 clang/test/Driver/arm-toolchain.c  | 12 
 clang/test/Driver/riscv32-toolchain.c  | 13 +
 clang/test/Driver/riscv64-toolchain.c  | 13 +
 6 files changed, 68 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index e670696cd59ae..c98b12d73f55f 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -604,6 +604,12 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
 
   CmdArgs.push_back("-Bstatic");
+  if (IsStaticPIE) {
+CmdArgs.push_back("-pie");
+CmdArgs.push_back("--no-dynamic-linker");
+CmdArgs.push_back("-z");
+CmdArgs.push_back("text");
+  }
 
   if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
 CmdArgs.push_back("-m");
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index bdd77ac84913c..f8f97b02a5f95 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2089,6 +2089,18 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const 
ArgList &Args) {
   return std::make_tuple(RelocM, 0U, false);
 }
 
+bool tools::getStaticPIE(const ArgList &Args, const ToolChain &TC) {
+  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
+  if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
+const Driver &D = TC.getDriver();
+const llvm::opt::OptTable &Opts = D.getOpts();
+StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
+StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
+D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
+  }
+  return HasStaticPIE;
+}
+
 // `-falign-functions` indicates that the functions should be aligned to the
 // backend's preferred alignment.
 //
diff --git a/clang/test/Driver/aarch64-toolchain.c 
b/clang/test/Driver/aarch64-toolchain.c
index cfad4b8eb6829..4dc970f0e8685 100644
--- a/clang/test/Driver/aarch64-toolchain.c
+++ b/clang/test/Driver/aarch64-toolchain.c
@@ -157,3 +157,15 @@
 // AARCH64-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtbegin.o"
 // AARCH64-BAREMETAL-UNWINDLIB: "--start-group" 
"{{.*}}libclang_rt.builtins{{.*}}.a" "--as-needed" "-lunwind" "--no-as-needed" 
"-lc" "-lgloss" "--end-group"
 // AARCH64-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtend.o"
+
+// RUN: %clang -static-pie -### %s -fuse-ld= \
+// RUN:   --target=aarch64-none-elf --rtlib=libgcc --unwindlib=platform \
+// RUN:   --gcc-toolchain=%S/Inputs/basic_aarch64_gcc_tree \
+// RUN:   --sysroot=%S/Inputs/basic_arm_gcc_tree/aarch64-none-elf 2>&1 \
+// RUN:   | FileCheck -check-prefix=C-ARM-STATIC-PIE %s
+
+// C-ARM-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" 
"aarch64linux" "-EL"
+// C-ARM-STATIC-PIE: "{{.*}}rcrt1.o"
+// C-ARM-STATIC-PIE: "{{.*}}crtbeginS.o"
+// C-ARM-STATIC-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "-lgloss" 
"--end-group"
+// C-ARM-STATIC-PIE: "{{.*}}crtendS.o"
diff --git a/clang/test/Driver/arm-toolchain.c 
b/clang/test/Driver/arm-toolchain.c
index c367594b0a758..f164fee723e25 100644
--- a/clang/test/Driver/arm-toolchain.c
+++ b/clang/test/Driver/arm-toolchain.c
@@ -158,3 +158,15 @@
 // ARM-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtbegin.o"
 // ARM-BAREMETAL-UNWINDLIB: "--start-group" "{{.*}}libclang_rt.builtins.a" 
"--as-needed" "-lunwind" "--no-as-needed" "-lc" "-lgloss" "--end-group"
 // ARM-BAREMETAL-UNWINDLIB: "{{.*}}clang_rt.crtend.o"
+
+// RUN: %clang -static-pie -### %s -fuse-ld= \
+// RUN:   --target=armv6m-none-eabi --rtlib=libgcc --unwindlib=platform \
+// RUN:   --gcc-toolchain=%S/Inputs/basic_arm_gcc_tree \
+// RUN:   --sysroot=%S/Inputs/basic_arm_gcc_tree/armv6m-none-eabi 2>&1 \
+// RUN:   | FileCheck -check-prefix=C-ARM-STATIC-PIE %s
+
+// C-ARM-STATIC-PIE: "-Bstatic" "-pie" "--no-dynamic-linker" "-z" "text" "-m" 
"armelf_linux_eabi" "-EL"
+// C-ARM-STATIC-PIE: "{{.*}}rcrt1.o"
+// C-ARM-STATIC-PIE: "{{.*}}crtbeginS.o"
+// C-ARM-STATIC-PIE: "--start-group" "-lgcc" "-lgcc_eh" "-lc" "-lgloss" 
"--end-group"
+// C-ARM-STATIC-PIE: "{{.*}}crtendS.o"
diff --git a/clang/test/Driver/riscv32-toolchain.c 
b/clang/test/Driver/riscv32-toolchain.c
index 8cf20aa592a3a..f54e6cf8a6c03 100644
--- a/clang/test/Driver/riscv32-toolchain.c
+++ b/clang/test/Driver/

[clang] Propogate linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-08 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/147589

None

>From 6f96fc844751a95ee18b69ddb850ddd2fe76a89c Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 8 Jul 2025 07:21:37 -0700
Subject: [PATCH 1/2] Propogate appropriate linker flags for `-static-pie` in
 BareMetal toolchain

Change-Id: I2ad7cd9d2677334de21ae8abade874ca7b7106b5
---
 clang/include/clang/Driver/CommonArgs.h|  2 ++
 clang/lib/Driver/ToolChains/BareMetal.cpp  | 19 
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 12 ++
 clang/lib/Driver/ToolChains/Gnu.cpp| 12 --
 clang/test/Driver/baremetal.cpp| 26 ++
 5 files changed, 55 insertions(+), 16 deletions(-)

diff --git a/clang/include/clang/Driver/CommonArgs.h 
b/clang/include/clang/Driver/CommonArgs.h
index 26aa3ccf84786..d8877903c892f 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -85,6 +85,8 @@ const char *RelocationModelName(llvm::Reloc::Model Model);
 std::tuple
 ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args);
 
+bool getStaticPIE(const llvm::opt::ArgList &Args, const ToolChain &TC);
+
 unsigned ParseFunctionAlignment(const ToolChain &TC,
 const llvm::opt::ArgList &Args);
 
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index e670696cd59ae..6ee3fbe400566 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -599,11 +599,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   const Driver &D = getToolChain().getDriver();
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
+  const bool IsStaticPIE = getStaticPIE(Args, TC);
 
   if (!D.SysRoot.empty())
 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
 
   CmdArgs.push_back("-Bstatic");
+  if(IsStaticPIE) {
+CmdArgs.push_back("-pie");
+CmdArgs.push_back("--no-dynamic-linker");
+CmdArgs.push_back("-z");
+CmdArgs.push_back("text");
+  }
 
   if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
 CmdArgs.push_back("-m");
@@ -633,14 +640,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 
   const char *CRTBegin, *CRTEnd;
   if (NeedCRTs) {
-if (!Args.hasArg(options::OPT_r))
-  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
+if (!Args.hasArg(options::OPT_r)) {
+  const char *crt = "crt0.o";
+  if (IsStaticPIE)
+crt = "rcrt1.o";
+  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(crt)));
+}
 if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
   auto RuntimeLib = TC.GetRuntimeLibType(Args);
   switch (RuntimeLib) {
   case (ToolChain::RLT_Libgcc): {
-CRTBegin = "crtbegin.o";
-CRTEnd = "crtend.o";
+CRTBegin = IsStaticPIE ? "crtbeginS.o" : "crtbegin.o";
+CRTEnd = IsStaticPIE ? "crtendS.o" : "crtend.o";
 break;
   }
   case (ToolChain::RLT_CompilerRT): {
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index bdd77ac84913c..9e16cba0c7e25 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2089,6 +2089,18 @@ tools::ParsePICArgs(const ToolChain &ToolChain, const 
ArgList &Args) {
   return std::make_tuple(RelocM, 0U, false);
 }
 
+bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
+  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
+  if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
+const Driver &D = TC.getDriver();
+const llvm::opt::OptTable &Opts = D.getOpts();
+StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
+StringRef NoPIEName = Opts.getOptionName(options::OPT_nopie);
+D.Diag(diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
+  }
+  return HasStaticPIE;
+}
+
 // `-falign-functions` indicates that the functions should be aligned to the
 // backend's preferred alignment.
 //
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp 
b/clang/lib/Driver/ToolChains/Gnu.cpp
index f5e2655857432..01b146db24f3e 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -219,18 +219,6 @@ void tools::gcc::Linker::RenderExtraToolArgs(const 
JobAction &JA,
   // The types are (hopefully) good enough.
 }
 
-static bool getStaticPIE(const ArgList &Args, const ToolChain &TC) {
-  bool HasStaticPIE = Args.hasArg(options::OPT_static_pie);
-  if (HasStaticPIE && Args.hasArg(options::OPT_no_pie)) {
-const Driver &D = TC.getDriver();
-const llvm::opt::OptTable &Opts = D.getOpts();
-StringRef StaticPIEName = Opts.getOptionName(options::OPT_static_pie);
-StringRef NoPIEName = Opts.getOptionNam

[clang] Add necessary linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-10 Thread Garvit Gupta via cfe-commits


@@ -599,11 +599,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   const Driver &D = getToolChain().getDriver();
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
+  const bool IsStaticPIE = getStaticPIE(Args, TC);
 
   if (!D.SysRoot.empty())
 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
 
   CmdArgs.push_back("-Bstatic");
+  if (IsStaticPIE) {
+CmdArgs.push_back("-pie");
+CmdArgs.push_back("--no-dynamic-linker");

quic-garvgupt wrote:

This change is needed for both LLD and GNU LD. I dont see `static -pie` flags 
being passed by default for LLD if `-static-pie` is passed to clang driver. 
Moreover, without this change we get a unused argument warning as well.
Pls see below for verbose logs.

`clang --target=aarch64-none-elf -fuse-ld=lld -static-pie empty.c -###`

`clang: warning: argument unused during compilation: '-static-pie' 
[-Wunused-command-line-argument]`
`...`
`ld.lld" "-Bstatic" "-m" "aarch64linux" "-EL" "crt0.o" 
"-L/prj/llvm-arm/hexbuild_home/nightly/install/community-mainline/cross/2025-07-09/bin/../lib/clang-runtimes/aarch64-none-elf/lib"
 
"-L/prj/llvm-arm/hexbuild_home/nightly/install/community-mainline/cross/2025-07-09/lib/clang/21/lib/aarch64-unknown-none-elf"
 
"-L/prj/llvm-arm/hexbuild_home/nightly/install/community-mainline/cross/2025-07-09/bin/../lib/clang-runtimes/aarch64-none-elf/lib"
 "/tmp/empty-09660d.o" "--start-group" 
"/prj/llvm-arm/hexbuild_home/nightly/install/community-mainline/cross/2025-07-09/lib/clang/21/lib/aarch64-unknown-none-elf/libclang_rt.builtins.a"
 "-lc" "--end-group" "-o" "a.out"`

Pls let me know if I missed something in your comment or interpreted it 
differently.

https://github.com/llvm/llvm-project/pull/147589
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add necessary linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-10 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/147589
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver][RISCV] Fix incorrect compiler-rt path override in BareMetal toolchain after RISCVToolChain removal (PR #146849)

2025-07-03 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/146849
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver][RISCV] Fix incorrect compiler-rt path override in BareMetal toolchain after RISCVToolChain removal (PR #146849)

2025-07-03 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/146849
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver][RISCV] Fix and print appropriate compiler-rt path when GCCInstallation is valid (PR #146849)

2025-07-03 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/146849

Baremetal and RISCVToolchain used different paths for clangrt builtins
relative to resource-dir before RISCVToolChain got removed in commit f8cb798
This created a discrepancy in behavior for riscv target when GCCInstallation is
valid. This patch fixes this difference in behavior.

After this fix -
With valid GCCInstallation
//lib/libclang_rt.builtins.a

Without valid GCCInstallation
//lib/baremetal/libclang_rt.builtins-.a



>From f46d7cce35748114997de7264291f6dc004d1dc9 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Thu, 3 Jul 2025 02:56:01 -0700
Subject: [PATCH] [Driver][RISCV] Fix and print appropriate compiler-rt path
 when GCCInstallation is valid

RISCVToolChain which recently got removed in commit f8cb798, used a
different compiler-rt path relative to resource-dir. However,
this behavior got override when it was merged in BareMetal toolchain.

This patch fixes that.

Without valid GCCInstallation:
//lib/baremetal/libclang_rt.builtins-.a

With valid GCCInstalltion:
//lib/libclang_rt.builtins.a

Change-Id: I37670b24019e3d473139ad6df8c3e6b92196fa6d
---
 clang/lib/Driver/ToolChains/BareMetal.cpp  | 9 +
 clang/lib/Driver/ToolChains/BareMetal.h| 1 +
 clang/test/Driver/print-libgcc-file-name-clangrt.c | 9 +
 3 files changed, 19 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index 981395deb9dbc..cd4c3104b6deb 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -206,6 +206,15 @@ std::string BareMetal::computeSysRoot() const {
   return computeClangRuntimesSysRoot(D, /*IncludeTriple*/ true);
 }
 
+std::string BareMetal::getCompilerRTPath() const {
+  if (IsGCCInstallationValid || detectGCCToolchainAdjacent(getDriver())) {
+SmallString<128> Path(getDriver().ResourceDir);
+llvm::sys::path::append(Path, "lib");
+return std::string(Path.str());
+  }
+  return ToolChain::getCompilerRTPath();
+}
+
 static void addMultilibsFilePaths(const Driver &D, const MultilibSet 
&Multilibs,
   const Multilib &Multilib,
   StringRef InstallPath,
diff --git a/clang/lib/Driver/ToolChains/BareMetal.h 
b/clang/lib/Driver/ToolChains/BareMetal.h
index cc57fa21867a2..d3d415b337a0b 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.h
+++ b/clang/lib/Driver/ToolChains/BareMetal.h
@@ -76,6 +76,7 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public Generic_ELF {
   addLibStdCxxIncludePaths(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const override;
   std::string computeSysRoot() const override;
+  std::string getCompilerRTPath() const override;
   SanitizerMask getSupportedSanitizers() const override;
 
   SmallVector
diff --git a/clang/test/Driver/print-libgcc-file-name-clangrt.c 
b/clang/test/Driver/print-libgcc-file-name-clangrt.c
index a902eedc85209..e376d469cad6b 100644
--- a/clang/test/Driver/print-libgcc-file-name-clangrt.c
+++ b/clang/test/Driver/print-libgcc-file-name-clangrt.c
@@ -63,3 +63,12 @@
 // RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-ARM-BAREMETAL-PER-TARGET %s
 // CHECK-CLANGRT-ARM-BAREMETAL-PER-TARGET: libclang_rt.builtins.a
+
+// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
+// RUN: --target=riscv32-unknown-elf \
+// RUN: --sysroot=%S/Inputs/resource_dir_with_arch_subdir \
+// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
+// RUN: --gcc-toolchain=%S/Inputs/basic_riscv32_tree 2>&1 \
+// RUN:| FileCheck --check-prefix=CHECK-RISCV-BAREMETALTOOLCHAIN %s
+// CHECK-RISCV-BAREMETALTOOLCHAIN-NOT: 
baremetal{{/|\\}}libclang_rt.builtins-riscv32.a
+// CHECK-RISCV-BAREMETALTOOLCHAIN: libclang_rt.builtins.a

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


[clang] [Driver][RISCV] Fix and print appropriate compiler-rt path when GCCInstallation is valid (PR #146849)

2025-07-03 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/146849
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland [Driver] Fix link order of BareMetal toolchain object (PR #146291)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/146291
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Integrate RISCV target in baremetal toolchain object and deprecate RISCVToolchain object (PR #121831)

2025-06-30 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/121831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV][Driver] Add support for `-m` flag to linker job of Baremetal toolchain (PR #134442)

2025-06-30 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/134442
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Integrate RISCV target in baremetal toolchain object and deprecate RISCVToolchain object (PR #121831)

2025-06-30 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/121831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland[Driver] Add option to force undefined symbols during linking in BareMetal toolchain object. (PR #146304)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/146304
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Forward sysroot from Driver to linker in BareMetal ToolChain Object (PR #132808)

2025-06-30 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/132808
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV][Driver] Add support for `-m` flag to linker job of Baremetal toolchain (PR #134442)

2025-06-30 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/134442
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV][Driver] Add support for `-m` flag to linker job of Baremetal toolchain (PR #134442)

2025-06-30 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/134442

>From 9d48ea76ef90ac9e76ade4af77667e44652e3408 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Fri, 4 Apr 2025 12:51:19 -0700
Subject: [PATCH] [RISCV][Driver] Add support for `-m` flag to linker job of
 Baremetal toolchain.

Change-Id: Ifce8a3a7f1df9c12561d35ca3c923595e3619428
---
 clang/include/clang/Driver/CommonArgs.h|  2 +
 clang/lib/Driver/ToolChains/BareMetal.cpp  | 15 -
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 72 ++
 clang/lib/Driver/ToolChains/Gnu.cpp| 72 --
 clang/test/Driver/aarch64-toolchain.c  | 14 ++---
 clang/test/Driver/arm-toolchain.c  | 14 ++---
 clang/test/Driver/baremetal.cpp| 51 +++
 7 files changed, 127 insertions(+), 113 deletions(-)

diff --git a/clang/include/clang/Driver/CommonArgs.h 
b/clang/include/clang/Driver/CommonArgs.h
index ddb21c1e8a8b8..26aa3ccf84786 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -31,6 +31,8 @@ void AddLinkerInputs(const ToolChain &TC, const InputInfoList 
&Inputs,
  const llvm::opt::ArgList &Args,
  llvm::opt::ArgStringList &CmdArgs, const JobAction &JA);
 
+const char *getLDMOption(const llvm::Triple &T, const llvm::opt::ArgList 
&Args);
+
 void addLinkerCompressDebugSectionsOption(const ToolChain &TC,
   const llvm::opt::ArgList &Args,
   llvm::opt::ArgStringList &CmdArgs);
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index 84d9747488060..fd36a5e7f0af0 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -573,8 +573,19 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 
   CmdArgs.push_back("-Bstatic");
 
-  if (TC.getTriple().isRISCV() && Args.hasArg(options::OPT_mno_relax))
-CmdArgs.push_back("--no-relax");
+  if (const char *LDMOption = getLDMOption(TC.getTriple(), Args)) {
+CmdArgs.push_back("-m");
+CmdArgs.push_back(LDMOption);
+  } else {
+D.Diag(diag::err_target_unknown_triple) << Triple.str();
+return;
+  }
+
+  if (Triple.isRISCV()) {
+CmdArgs.push_back("-X");
+if (Args.hasArg(options::OPT_mno_relax))
+  CmdArgs.push_back("--no-relax");
+  }
 
   if (Triple.isARM() || Triple.isThumb()) {
 bool IsBigEndian = arm::isARMBigEndian(Triple, Args);
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 672b73432847d..070901f037823 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -527,6 +527,78 @@ void tools::AddLinkerInputs(const ToolChain &TC, const 
InputInfoList &Inputs,
   }
 }
 
+const char *tools::getLDMOption(const llvm::Triple &T, const ArgList &Args) {
+  switch (T.getArch()) {
+  case llvm::Triple::x86:
+if (T.isOSIAMCU())
+  return "elf_iamcu";
+return "elf_i386";
+  case llvm::Triple::aarch64:
+if (T.isOSManagarm())
+  return "aarch64managarm";
+return "aarch64linux";
+  case llvm::Triple::aarch64_be:
+return "aarch64linuxb";
+  case llvm::Triple::arm:
+  case llvm::Triple::thumb:
+  case llvm::Triple::armeb:
+  case llvm::Triple::thumbeb:
+return tools::arm::isARMBigEndian(T, Args) ? "armelfb_linux_eabi"
+   : "armelf_linux_eabi";
+  case llvm::Triple::m68k:
+return "m68kelf";
+  case llvm::Triple::ppc:
+if (T.isOSLinux())
+  return "elf32ppclinux";
+return "elf32ppc";
+  case llvm::Triple::ppcle:
+if (T.isOSLinux())
+  return "elf32lppclinux";
+return "elf32lppc";
+  case llvm::Triple::ppc64:
+return "elf64ppc";
+  case llvm::Triple::ppc64le:
+return "elf64lppc";
+  case llvm::Triple::riscv32:
+return "elf32lriscv";
+  case llvm::Triple::riscv64:
+return "elf64lriscv";
+  case llvm::Triple::sparc:
+  case llvm::Triple::sparcel:
+return "elf32_sparc";
+  case llvm::Triple::sparcv9:
+return "elf64_sparc";
+  case llvm::Triple::loongarch32:
+return "elf32loongarch";
+  case llvm::Triple::loongarch64:
+return "elf64loongarch";
+  case llvm::Triple::mips:
+return "elf32btsmip";
+  case llvm::Triple::mipsel:
+return "elf32ltsmip";
+  case llvm::Triple::mips64:
+if (tools::mips::hasMipsAbiArg(Args, "n32") || T.isABIN32())
+  return "elf32btsmipn32";
+return "elf64btsmip";
+  case llvm::Triple::mips64el:
+if (tools::mips::hasMipsAbiArg(Args, "n32") || T.isABIN32())
+  return "elf32ltsmipn32";
+return "elf64ltsmip";
+  case llvm::Triple::systemz:
+return "elf64_s390";
+  case llvm::Triple::x86_64:
+if (T.isX32())
+  return "elf32_x86_64";
+return "elf_x86_64";
+  case llvm::Triple::ve:
+return "elf64ve";
+  case llvm::Tri

[clang] Reland [Driver] Add support for crtbegin.o, crtend.o and libgloss lib to BareMetal toolchain object (PR #144649)

2025-06-30 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

> > Moreover, this patch makes gnuld the default linker for baremetal
> > toolchain object. User need to pass -fuse-ld=lld explicitly to driver to 
> > select
> > lld
> 
> This seems like an unrelated change, it was not mentioned in the RFC, and it 
> breaks things: baremetal toolchains generally should not be presumed to be 
> for the same architecture as host. On an x86_64 host, this results in 
> `/usr/bin/ld` being used for `clang --target=riscv64`, but `/usr/bin/ld` is 
> (generally) a linker that does not understand RISC-V targets or object files. 
> This seems like a bad idea, can this please be reverted?

This patch does not make assumption that it is for the same architecture as 
host. Rather, it is inline with other toolchain objects that supports 
GCCInstallation which have ld as the default linker. And this change is done 
only after reaching a consensus with lld and clang driver maintainer which can 
be found [here](https://github.com/llvm/llvm-project/pull/121830). The 
conclusion of that discussion was that if a user wants to use a different 
linker, they should do by specifying it explicitly via `-fuse-ld` or 
`CLANG_DEFAULT_LINKER`.

https://github.com/llvm/llvm-project/pull/144649
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland [Driver] Add support for crtbegin.o, crtend.o and libgloss lib to BareMetal toolchain object (PR #144649)

2025-06-30 Thread Garvit Gupta via cfe-commits


@@ -59,3 +118,39 @@
 
 // CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../aarch64-none-elf/include/c++/v1"
 // CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../aarch64-none-elf/include"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: "{{.*}}.o" "-Bstatic" "-EL"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../aarch64-none-elf/lib/crt0.o"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: 
"-L{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: 
"-L{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../aarch64-none-elf/lib"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: "-lc++" "-lm" "--start-group" 
"-lgcc_s" "-lgcc" "-lc" "-lgloss" "--end-group"
+// CXX-AARCH64-BAREMETAL-NOSYSROOT-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o"
+
+// RUN: %clang -### %s -fuse-ld= \
+// RUN:   --target=aarch64-none-elf --rtlib=compiler-rt --unwindlib=platform \
+// RUN:   --gcc-toolchain=%S/Inputs/basic_aarch64_gcc_tree \
+// RUN:   --sysroot=%S/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf 2>&1 \
+// RUN:   | FileCheck -check-prefix=AARCH64-BAREMETAL-COMPILER-RT %s
+
+// AARCH64-BAREMETAL-COMPILER-RT: "{{.*}}crt0.o"
+// AARCH64-BAREMETAL-COMPILER-RT: "{{.*}}clang_rt.crtbegin.o"
+// AARCH64-BAREMETAL-COMPILER-RT: "--start-group" 
"{{.*}}libclang_rt.builtins.a" "-lc" "-lgloss" "--end-group"

quic-garvgupt wrote:

>However, this does not reproduce for me locally, so maybe it's dependent on 
>some incremental build state.

Can you confirm if you are still seeing the error on your buildbots?

https://github.com/llvm/llvm-project/pull/144649
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland[Driver] Add option to force undefined symbols during linking in BareMetal toolchain object. (PR #146304)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/146304

Add support for `-u` option to force defined symbols. This option is supported 
by both lld and gnuld.

This is done as a part of the effort to merge RISCVToolchain object into 
BareMetal toolchain object.

This is the 4th patch in the series of patches for merging RISCVToolchain 
object into BareMetal toolchain object.

RFC:
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

>From 2a8f6609095561a02e900f3ed077d32cbd3ce628 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 18 Jun 2025 00:28:23 +0530
Subject: [PATCH] [Driver] Add option to force undefined symbols during linking
 in BareMetal toolchain object. (#132807)

Add support for `-u` option to force defined symbols. This option is
supported by both lld and gnuld.

This is done as a part of the effort to merge RISCVToolchain object into
BareMetal toolchain object.

This is the 4th patch in the series of patches for merging
RISCVToolchain object into BareMetal toolchain object.

RFC:
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
---
 clang/lib/Driver/ToolChains/BareMetal.cpp   |  5 +++--
 clang/test/Driver/baremetal-undefined-symbols.c | 14 ++
 clang/test/Driver/riscv-args.c  |  6 --
 3 files changed, 17 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/Driver/baremetal-undefined-symbols.c
 delete mode 100644 clang/test/Driver/riscv-args.c

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index a665040662a3f..d4e4e6d04b417 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -609,8 +609,9 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 }
   }
 
-  Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
-options::OPT_s, options::OPT_t, options::OPT_r});
+  Args.addAllArgs(CmdArgs,
+  {options::OPT_L, options::OPT_u, options::OPT_T_Group,
+   options::OPT_s, options::OPT_t, options::OPT_r});
 
   TC.AddFilePathLibArgs(Args, CmdArgs);
 
diff --git a/clang/test/Driver/baremetal-undefined-symbols.c 
b/clang/test/Driver/baremetal-undefined-symbols.c
new file mode 100644
index 0..bff58c7c54c33
--- /dev/null
+++ b/clang/test/Driver/baremetal-undefined-symbols.c
@@ -0,0 +1,14 @@
+// Check the arguments are correctly passed
+
+// Make sure -T is the last with gcc-toolchain option
+// RUN: %clang -### --target=riscv32 --gcc-toolchain= -Xlinker --defsym=FOO=10 
-T a.lds -u foo %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-LD %s
+// CHECK-LD: {{.*}} "--defsym=FOO=10" {{.*}} "-u" "foo" {{.*}} "-T" "a.lds"
+
+// TODO: Merge this test with the above in the last patch when finally 
integrating riscv
+// Make sure -T is the last with gcc-toolchain option
+// RUN: %clang -### --target=aarch64-none-elf --gcc-toolchain= -Xlinker 
--defsym=FOO=10 -T a.lds -u foo %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-ARM-LD %s
+// RUN: %clang -### --target=armv6m-none-eabi --gcc-toolchain= -Xlinker 
--defsym=FOO=10 -T a.lds -u foo %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-ARM-LD %s
+// CHECK-ARM-LD: {{.*}} "-T" "a.lds" "-u" "foo" {{.*}} "--defsym=FOO=10"
diff --git a/clang/test/Driver/riscv-args.c b/clang/test/Driver/riscv-args.c
deleted file mode 100644
index cab08e5b0f811..0
--- a/clang/test/Driver/riscv-args.c
+++ /dev/null
@@ -1,6 +0,0 @@
-// Check the arguments are correctly passed
-
-// Make sure -T is the last with gcc-toolchain option
-// RUN: %clang -### --target=riscv32 --gcc-toolchain= -Xlinker --defsym=FOO=10 
-T a.lds -u foo %s 2>&1 \
-// RUN:   | FileCheck -check-prefix=CHECK-LD %s
-// CHECK-LD: {{.*}} "--defsym=FOO=10" {{.*}} "-u" "foo" {{.*}} "-T" "a.lds"

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


[clang] Add necessary linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-15 Thread Garvit Gupta via cfe-commits


@@ -599,11 +599,18 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   const Driver &D = getToolChain().getDriver();
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
+  const bool IsStaticPIE = getStaticPIE(Args, TC);
 
   if (!D.SysRoot.empty())
 CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
 
   CmdArgs.push_back("-Bstatic");
+  if (IsStaticPIE) {
+CmdArgs.push_back("-pie");
+CmdArgs.push_back("--no-dynamic-linker");

quic-garvgupt wrote:

> think @petrhosek was asking specifically about the "--no-dynamic-linker" flag.

Thanks for clarifying this.

>I think for gnu ld it is necessary as it's possible to mix -static and -shared 
>according to the manpage. I presume this ensures that you get an error in that 
>case rather than a statically linked shared library.

Yes, if it’s the default behavior for LLD, then it would still be necessary for 
GNU LD. When both `-static` and `-shared` are used i.e., when creating a 
statically linked shared library, all definitions within the library are fully 
resolved at link time and pulled from static libraries. In such cases, there is 
no need for a dynamic linker.


https://github.com/llvm/llvm-project/pull/147589
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Add necessary linker flags when -static-pie is enabled in BareMetal Toolchain (PR #147589)

2025-07-15 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

>I don't think that this should necessarily prevent this patch from landing. To 
>some degree users can just not use the option if the toolchain doesn't support 
>it. 

The file names for crt files are borrowed from their linux equivalent. However 
as already pointed out, if a user wants to link against different set of `crt*` 
files, then they can pass `-nostdlib` or `-nostartfiles and provide their own 
implementation of startup routines.

>I'm interested in what the RISCV bare-metal toolchains do in this case?

In such scenarios, the responsibility for generating startup code with an 
appropriate relocation resolver is delegated to customer image vendors. They 
are expected to use options like -nostartfiles or -nostdlib, and provide their 
own startup routines that include an implementation of a custom relocation 
resolver. Additionally, they must define linker script symbols to delimit the 
dynamic relocations. The custom resolver then parses each .rela.dyn relocation 
entry within the boundaries defined by these symbols.
As far as I know, there is nothing RISC-V bare-metal-specific in the toolchains 
to handle such cases.

https://github.com/llvm/llvm-project/pull/147589
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix extraneous addition of `-X` flag in baremetal toolchain (PR #148855)

2025-07-15 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/148855

Commit 597ee88 moved the -X flag to a new position in the baremetal toolchain's 
linker job, 
but unintentionally left the original instance in place.

This patch removes the redundant flag, ensuring -X is passed only once.

>From 39063e7df808cdf86a0610918b034cf8dc9626e9 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 15 Jul 2025 07:08:23 -0700
Subject: [PATCH] Fix extraneous addition of `-X` flag in baremetal toolchain

Commit 597ee88 moved the -X flag to a new position in the baremetal
toolchain's linker job, but unintentionally left the original instance
in place.

This patch removes the redundant flag, ensuring -X is passed
only once.

Change-Id: I4318e6eba52e4664caed1c21a276210346b65405
---
 clang/lib/Driver/ToolChains/BareMetal.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index e670696cd59ae..497f3330237b9 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -694,9 +694,6 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   NeedCRTs)
 CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(CRTEnd)));
 
-  if (TC.getTriple().isRISCV())
-CmdArgs.push_back("-X");
-
   // The R_ARM_TARGET2 relocation must be treated as R_ARM_REL32 on arm*-*-elf
   // and arm*-*-eabi (the default is R_ARM_GOT_PREL, used on arm*-*-linux and
   // arm*-*-*bsd).

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


[clang] Fix adding support for -X flag in Baremetal toolchain (PR #147804)

2025-07-15 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/147804

>From d92b98015db0e03d1f71658884cd931162835184 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 9 Jul 2025 11:58:19 -0700
Subject: [PATCH] Fix adding support for -X flag in Baremetal toolchain

Commit 597ee88 made adding -X flag conditional on riscv target however
it was incorrect and was an oversight. This patch fixes this issue

Change-Id: I862b2775711951c9af5f3ad8722277e4df72033b
---
 clang/lib/Driver/ToolChains/BareMetal.cpp |  2 +-
 clang/test/Driver/aarch64-toolchain.c | 14 -
 clang/test/Driver/arm-toolchain.c | 14 -
 clang/test/Driver/baremetal.cpp   | 38 +++
 4 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index e670696cd59ae..e212b59790c7e 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -613,8 +613,8 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 return;
   }
 
+  CmdArgs.push_back("-X");
   if (Triple.isRISCV()) {
-CmdArgs.push_back("-X");
 if (Args.hasArg(options::OPT_mno_relax))
   CmdArgs.push_back("--no-relax");
   }
diff --git a/clang/test/Driver/aarch64-toolchain.c 
b/clang/test/Driver/aarch64-toolchain.c
index cfad4b8eb6829..1765946b05aff 100644
--- a/clang/test/Driver/aarch64-toolchain.c
+++ b/clang/test/Driver/aarch64-toolchain.c
@@ -11,7 +11,7 @@
 // LLD-AARCH64-BAREMETAL: "-isysroot" 
"{{.*}}Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
 // LLD-AARCH64-BAREMETAL: "-internal-isystem" 
"{{.*}}Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include"
 // LLD-AARCH64-BAREMETAL: "{{.*}}/Inputs/lld/ld.lld"
-// LLD-AARCH64-BAREMETAL: "-Bstatic" "-m" "aarch64linux" "-EL"
+// LLD-AARCH64-BAREMETAL: "-Bstatic" "-m" "aarch64linux" "-X" "-EL"
 // LLD-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o"
 // LLD-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
 // LLD-AARCH64-BAREMETAL: 
"-L{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1"
@@ -30,7 +30,7 @@
 // C-AARCH64-BAREMETAL: "-internal-isystem" 
"{{.*}}Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include"
 // C-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld"
 // C-AARCH64-BAREMETAL: 
"--sysroot={{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
-// C-AARCH64-BAREMETAL: "-Bstatic" "-m" "aarch64linux" "-EL"
+// C-AARCH64-BAREMETAL: "-Bstatic" "-m" "aarch64linux" "-X" "-EL"
 // C-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o"
 // C-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
 // C-AARCH64-BAREMETAL: 
"-L{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1"
@@ -47,7 +47,7 @@
 // C-AARCH64-BAREMETAL-NOSYSROOT: "-cc1" "-triple" "aarch64-unknown-none-elf"
 // C-AARCH64-BAREMETAL-NOSYSROOT: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../aarch64-none-elf/include"
 // C-AARCH64-BAREMETAL-NOSYSROOT: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld"
-// C-AARCH64-BAREMETAL-NOSYSROOT: "-Bstatic" "-m" "aarch64linux" "-EL"
+// C-AARCH64-BAREMETAL-NOSYSROOT: "-Bstatic" "-m" "aarch64linux" "-X" "-EL"
 // C-AARCH64-BAREMETAL-NOSYSROOT: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../aarch64-none-elf/lib/crt0.o"
 // C-AARCH64-BAREMETAL-NOSYSROOT: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
 // C-AARCH64-BAREMETAL-NOSYSROOT: 
"-L{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1"
@@ -67,7 +67,7 @@
 // CXX-AARCH64-BAREMETAL: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include"
 // CXX-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld"
 // CXX-AARCH64-BAREMETAL: 
"--sysroot={{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
-// CXX-AARCH64-BAREMETAL: "-Bstatic" "-m" "aarch64linux" "-EL"
+// CXX-AARCH64-BAREMETAL: "-Bstatic" "-m" "aarch64linux" "-X" "-EL"
 // CXX-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o"
 // CXX-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
 // CXX-AARCH64-BAREMETAL: 
"-L{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1"
@@ -86,7 +86,7 @@
 // CXX-AARCH64-BAREMETAL-NOSYSROOT: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../aarch64-none-elf/include/c++/8.2.1"
 // CXX-AARCH64-BAREMETAL-NOSYSROOT: "-internal-isystem" 
"{{.*

[clang] Fix adding support for -X flag in Baremetal toolchain (PR #147804)

2025-07-15 Thread Garvit Gupta via cfe-commits


@@ -613,8 +613,8 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 return;
   }
 
+  CmdArgs.push_back("-X");

quic-garvgupt wrote:

I made a few mistakes in the commit message and title. The intent of this PR is 
to add support for passing the -X flag across all baremetal targets. While this 
flag has historically been used only for RISC-V, its functionality can be 
beneficial for ARM and AArch64 targets as well. The primary goal of this PR is 
to gather feedback on enabling this flag universally for baremetal toolchains, 
as it can help reduce the size of the .symtab section.

Apologies if this has been brought up or  discussed in the past in some other 
patch or thread!

The removal of extraneous `-X` has been done in this PR 
https://github.com/llvm/llvm-project/pull/148855

https://github.com/llvm/llvm-project/pull/147804
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland [Driver] Add support for GCC installation detection in Baremetal toolchain (PR #145390)

2025-06-26 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/145390

>From 296df79cb9e9696daa926267c8a9a853930fdc46 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 17 Jun 2025 21:43:17 +0530
Subject: [PATCH] Reland [Driver] Add support for GCC installation detection in
 Baremetal toolchain

This patch introduces enhancements to the Baremetal toolchain to support
GCC toolchain detection.
- If the --gcc-install-dir or --gcc-toolchain options are provided and
point to valid paths, the sysroot is derived from those locations.
- If not, the logic falls back to the existing sysroot inference
mechanism already present in the Baremetal toolchain.
- Support for adding include paths for the libstdc++ library has also
been added.

Additionally, the restriction to always use the integrated assembler has
been removed. With a valid GCC installation, the GNU assembler can now
be used as well.

This patch currently updates and adds tests for the ARM target only.
RISC-V-specific tests will be introduced in a later patch, once the
RISCVToolChain is fully merged into the Baremetal toolchain. At this
stage, there is no way to test the RISC-V target within this PR.

RFC:
https: 
//discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

This PR include fixes to tests which were failing on fuchsia builders

Change-Id: I5cd1d949297fee025b62ea6037c554bd4a0eeda1
---
 clang/docs/Toolchain.rst  |   5 +
 .../clang/Basic/DiagnosticDriverKinds.td  |   3 +
 clang/lib/Driver/ToolChains/BareMetal.cpp | 235 +-
 clang/lib/Driver/ToolChains/BareMetal.h   |  19 +-
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 clang/test/Driver/aarch64-gnutools.c  |   4 +
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 +++
 clang/test/Driver/aarch64-toolchain.c |  61 +
 clang/test/Driver/arm-gnutools.c  |   6 +
 clang/test/Driver/arm-toolchain-extra.c   |  29 +++
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 ++
 clang/test/Driver/check-no-multlib-warning.c  |  10 +
 32 files changed, 418 insertions(+), 64 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100755 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/bin/aarch64-none-elf-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o
 create mode 100755 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/bin/aarch64-none-elf-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100755 
clang/test/Driver/Inputs/basic_arm_gcc_tree/bin/armv6m-none-eabi-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/armv6m-none-eabi/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_arm_nogcc_tree/arm

[clang] Reland [Driver] Add support for crtbegin.o, crtend.o and libgloss lib to BareMetal toolchain object (PR #144649)

2025-06-26 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/144649

>From 36f8e160dfad059fdada0a00b5cdc20dde0076c0 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 17 Jun 2025 22:49:55 +0530
Subject: [PATCH] [Driver] Add support for crtbegin.o, crtend.o and libgloss
 lib to BareMetal toolchain object (#121830)

This patch conditionalise the addition of crt{begin,end}.o object files
along
with addition of -lgloss lib based on whether libc selected is newlib or
llvm
libc. Since there is no way a user can specify which libc it wants to
link
against, currently passing valid GCCInstallation to driver will select
newlib
otherwise it will default to llvm libc.

Moreover, this patch makes gnuld the default linker for baremetal
toolchain
object. User need to pass `-fuse-ld=lld` explicitly to driver to select
lld

This is the 2nd patch in the series of patches of merging RISCVToolchain
into
BareMetal toolchain object.

RFC:

https: 
//discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
Change-Id: I19f10807b5b41a8ebc413ca0b3209efd258a87df
---
 clang/lib/Driver/ToolChains/BareMetal.cpp   |  37 -
 clang/lib/Driver/ToolChains/BareMetal.h |   6 +-
 clang/test/Driver/aarch64-toolchain-extra.c |  35 +
 clang/test/Driver/aarch64-toolchain.c   | 156 +++
 clang/test/Driver/arm-toolchain-extra.c |  36 +
 clang/test/Driver/arm-toolchain.c   | 157 
 clang/test/Driver/baremetal.cpp |   3 +-
 clang/test/Driver/sanitizer-ld.c|   2 +-
 8 files changed, 423 insertions(+), 9 deletions(-)
 create mode 100644 clang/test/Driver/aarch64-toolchain-extra.c
 create mode 100644 clang/test/Driver/aarch64-toolchain.c
 create mode 100644 clang/test/Driver/arm-toolchain-extra.c
 create mode 100644 clang/test/Driver/arm-toolchain.c

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index d8168ed15febd..6abe537d92d12 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -471,9 +471,31 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 CmdArgs.push_back(Arch == llvm::Triple::aarch64_be ? "-EB" : "-EL");
   }
 
-  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles,
-   options::OPT_r)) {
-CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
+  bool NeedCRTs =
+  !Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles);
+
+  const char *CRTBegin, *CRTEnd;
+  if (NeedCRTs) {
+if (!Args.hasArg(options::OPT_r))
+  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath("crt0.o")));
+if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) {
+  auto RuntimeLib = TC.GetRuntimeLibType(Args);
+  switch (RuntimeLib) {
+  case (ToolChain::RLT_Libgcc): {
+CRTBegin = "crtbegin.o";
+CRTEnd = "crtend.o";
+break;
+  }
+  case (ToolChain::RLT_CompilerRT): {
+CRTBegin =
+TC.getCompilerRTArgString(Args, "crtbegin", ToolChain::FT_Object);
+CRTEnd =
+TC.getCompilerRTArgString(Args, "crtend", ToolChain::FT_Object);
+break;
+  }
+  }
+  CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(CRTBegin)));
+}
   }
 
   Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
@@ -496,15 +518,22 @@ void baremetal::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
   }
 
   if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
+CmdArgs.push_back("--start-group");
 AddRunTimeLibs(TC, D, CmdArgs, Args);
-
 CmdArgs.push_back("-lc");
+if (TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D))
+  CmdArgs.push_back("-lgloss");
+CmdArgs.push_back("--end-group");
   }
 
   if (D.isUsingLTO())
 addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
   D.getLTOMode() == LTOK_Thin);
 
+  if ((TC.hasValidGCCInstallation() || detectGCCToolchainAdjacent(D)) &&
+  NeedCRTs)
+CmdArgs.push_back(Args.MakeArgString(TC.GetFilePath(CRTEnd)));
+
   if (TC.getTriple().isRISCV())
 CmdArgs.push_back("-X");
 
diff --git a/clang/lib/Driver/ToolChains/BareMetal.h 
b/clang/lib/Driver/ToolChains/BareMetal.h
index f6295bda0a6a2..8e3799aff8e02 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.h
+++ b/clang/lib/Driver/ToolChains/BareMetal.h
@@ -35,7 +35,9 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
   Tool *buildStaticLibTool() const override;
 
 public:
-  bool useIntegratedAs() const override { return true; }
+  bool initGCCInstallation(const llvm::Triple &Triple,
+   const llvm::opt::ArgList &Args);
+  bool hasValidGCCInstallation() const { return IsGCCInstallationValid; }
   bool isBareMetal() const override { return true; }
   bool isCrossCompiling() const override { return true; }
   bool HasNativeLLVMSupport() const o

[clang] [RISCV] Integrate RISCV target in baremetal toolchain object and deprecate RISCVToolchain object (PR #121831)

2025-06-30 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/121831

>From b9e1db372117d9d418d0812a09874526e88efad4 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Mon, 6 Jan 2025 10:05:08 -0800
Subject: [PATCH] [RISCV] Integrate RISCV target in baremetal toolchain object
 and deprecate RISCVToolchain object

This patch:
- Adds CXXStdlib, runtimelib and unwindlib defaults for riscv target to
  BareMetal toolchain object.
- Add riscv 32 and 64-bit emulation flags to linker job of BareMetal
  toolchain.
- Removes call to RISCVToolChain object from llvm.

This PR is last patch in the series of patches of merging RISCVToolchain
object into BareMetal toolchain object.

RFC:
https: 
//discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
Change-Id: Ic5d64a4ed3ebc58c30c12d9827e7e57a02eb13ca
---
 clang/lib/Driver/CMakeLists.txt   |   1 -
 clang/lib/Driver/Driver.cpp   |  10 +-
 clang/lib/Driver/ToolChains/BareMetal.cpp |  22 ++
 clang/lib/Driver/ToolChains/BareMetal.h   |  10 +-
 .../lib/Driver/ToolChains/RISCVToolchain.cpp  | 231 --
 clang/lib/Driver/ToolChains/RISCVToolchain.h  |  67 -
 .../test/Driver/baremetal-undefined-symbols.c |  14 +-
 clang/test/Driver/riscv32-toolchain-extra.c   |   7 +-
 clang/test/Driver/riscv32-toolchain.c |  26 +-
 clang/test/Driver/riscv64-toolchain-extra.c   |   7 +-
 clang/test/Driver/riscv64-toolchain.c |  20 +-
 11 files changed, 62 insertions(+), 353 deletions(-)
 delete mode 100644 clang/lib/Driver/ToolChains/RISCVToolchain.cpp
 delete mode 100644 clang/lib/Driver/ToolChains/RISCVToolchain.h

diff --git a/clang/lib/Driver/CMakeLists.txt b/clang/lib/Driver/CMakeLists.txt
index 3cfd671e9d8f2..0268ec49eaf11 100644
--- a/clang/lib/Driver/CMakeLists.txt
+++ b/clang/lib/Driver/CMakeLists.txt
@@ -76,7 +76,6 @@ add_clang_library(clangDriver
   ToolChains/OHOS.cpp
   ToolChains/OpenBSD.cpp
   ToolChains/PS4CPU.cpp
-  ToolChains/RISCVToolchain.cpp
   ToolChains/Solaris.cpp
   ToolChains/SPIRV.cpp
   ToolChains/SPIRVOpenMP.cpp
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 0c5503a66b8f0..775f3f029861c 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -43,7 +43,6 @@
 #include "ToolChains/PPCFreeBSD.h"
 #include "ToolChains/PPCLinux.h"
 #include "ToolChains/PS4CPU.h"
-#include "ToolChains/RISCVToolchain.h"
 #include "ToolChains/SPIRV.h"
 #include "ToolChains/SPIRVOpenMP.h"
 #include "ToolChains/SYCL.h"
@@ -6955,16 +6954,11 @@ const ToolChain &Driver::getToolChain(const ArgList 
&Args,
 TC = std::make_unique(*this, Target, Args);
 break;
   case llvm::Triple::msp430:
-TC =
-std::make_unique(*this, Target, Args);
+TC = std::make_unique(*this, Target, 
Args);
 break;
   case llvm::Triple::riscv32:
   case llvm::Triple::riscv64:
-if (toolchains::RISCVToolChain::hasGCCToolchain(*this, Args))
-  TC =
-  std::make_unique(*this, Target, 
Args);
-else
-  TC = std::make_unique(*this, Target, Args);
+TC = std::make_unique(*this, Target, Args);
 break;
   case llvm::Triple::ve:
 TC = std::make_unique(*this, Target, Args);
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index fd36a5e7f0af0..981395deb9dbc 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -375,6 +375,28 @@ BareMetal::OrderedMultilibs 
BareMetal::getOrderedMultilibs() const {
   return llvm::reverse(Default);
 }
 
+ToolChain::CXXStdlibType BareMetal::GetDefaultCXXStdlibType() const {
+  if (getTriple().isRISCV() && IsGCCInstallationValid)
+return ToolChain::CST_Libstdcxx;
+  return ToolChain::CST_Libcxx;
+}
+
+ToolChain::RuntimeLibType BareMetal::GetDefaultRuntimeLibType() const {
+  if (getTriple().isRISCV() && IsGCCInstallationValid)
+return ToolChain::RLT_Libgcc;
+  return ToolChain::RLT_CompilerRT;
+}
+
+// TODO: Add a validity check for GCCInstallation.
+//   If valid, use `UNW_Libgcc`; otherwise, use `UNW_None`.
+ToolChain::UnwindLibType
+BareMetal::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
+  if (getTriple().isRISCV())
+return ToolChain::UNW_None;
+
+  return ToolChain::GetUnwindLibType(Args);
+}
+
 void BareMetal::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
   ArgStringList &CC1Args) const {
   if (DriverArgs.hasArg(options::OPT_nostdinc))
diff --git a/clang/lib/Driver/ToolChains/BareMetal.h 
b/clang/lib/Driver/ToolChains/BareMetal.h
index 54805530bae82..cc57fa21867a2 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.h
+++ b/clang/lib/Driver/ToolChains/BareMetal.h
@@ -56,13 +56,11 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public 
Generic_ELF {
 return UnwindTableLevel::None;
   }
 
-  RuntimeLibType GetDefaultRuntimeLibType() cons

[clang] [RISCV] Integrate RISCV target in baremetal toolchain object and deprecate RISCVToolchain object (PR #121831)

2025-06-30 Thread Garvit Gupta via cfe-commits


@@ -336,6 +336,26 @@ BareMetal::OrderedMultilibs 
BareMetal::getOrderedMultilibs() const {
   return llvm::reverse(Default);
 }
 
+ToolChain::CXXStdlibType BareMetal::GetDefaultCXXStdlibType() const {
+  if (getTriple().isRISCV() && GCCInstallation.isValid())
+return ToolChain::CST_Libstdcxx;
+  return ToolChain::CST_Libcxx;
+}
+
+ToolChain::RuntimeLibType BareMetal::GetDefaultRuntimeLibType() const {
+  if (getTriple().isRISCV() && GCCInstallation.isValid())
+return ToolChain::RLT_Libgcc;
+  return ToolChain::RLT_CompilerRT;
+}
+
+ToolChain::UnwindLibType
+BareMetal::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
+  if (getTriple().isRISCV())
+return ToolChain::UNW_None;

quic-garvgupt wrote:

Done

https://github.com/llvm/llvm-project/pull/121831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Integrate RISCV target in baremetal toolchain object and deprecate RISCVToolchain object (PR #121831)

2025-06-30 Thread Garvit Gupta via cfe-commits


@@ -336,6 +336,26 @@ BareMetal::OrderedMultilibs 
BareMetal::getOrderedMultilibs() const {
   return llvm::reverse(Default);
 }
 
+ToolChain::CXXStdlibType BareMetal::GetDefaultCXXStdlibType() const {
+  if (getTriple().isRISCV() && GCCInstallation.isValid())
+return ToolChain::CST_Libstdcxx;
+  return ToolChain::CST_Libcxx;
+}
+
+ToolChain::RuntimeLibType BareMetal::GetDefaultRuntimeLibType() const {
+  if (getTriple().isRISCV() && GCCInstallation.isValid())
+return ToolChain::RLT_Libgcc;
+  return ToolChain::RLT_CompilerRT;
+}
+
+ToolChain::UnwindLibType
+BareMetal::GetUnwindLibType(const llvm::opt::ArgList &Args) const {
+  if (getTriple().isRISCV())

quic-garvgupt wrote:

Done

https://github.com/llvm/llvm-project/pull/121831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Forward sysroot from Driver to linker in BareMetal ToolChain Object (PR #132808)

2025-06-29 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt updated 
https://github.com/llvm/llvm-project/pull/132808

>From e302490b6e6350951e75bd68578606aa7909692e Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Mon, 24 Mar 2025 07:04:59 -0700
Subject: [PATCH] [Driver] Forward sysroot from Driver to linker in BareMetal
 ToolChain Object

RISCVToolChain object passes `--sysroot` option from clang to gnuld. Adding
the supprt for the same in BareMetal toolchain object.

This is done as a part of the effort to merge RISCVToolchain object into
BareMetal toolchain object.

This is the 5th patch in the series of patches for merging RISCVToolchain object
into BareMetal toolchain object.

RFC:
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

Change-Id: Ie830bf6d126fea46dc225e5ef97e14349765ba07
---
 clang/lib/Driver/ToolChains/BareMetal.cpp |  3 +
 clang/test/Driver/aarch64-toolchain.c |  5 +-
 clang/test/Driver/arm-toolchain.c |  3 +
 clang/test/Driver/baremetal.cpp   | 96 +--
 4 files changed, 82 insertions(+), 25 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp 
b/clang/lib/Driver/ToolChains/BareMetal.cpp
index d4e4e6d04b417..84d9747488060 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -568,6 +568,9 @@ void baremetal::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   const llvm::Triple::ArchType Arch = TC.getArch();
   const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
 
+  if (!D.SysRoot.empty())
+CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
+
   CmdArgs.push_back("-Bstatic");
 
   if (TC.getTriple().isRISCV() && Args.hasArg(options::OPT_mno_relax))
diff --git a/clang/test/Driver/aarch64-toolchain.c 
b/clang/test/Driver/aarch64-toolchain.c
index 19afbcf278c17..0cdd04f2abef4 100644
--- a/clang/test/Driver/aarch64-toolchain.c
+++ b/clang/test/Driver/aarch64-toolchain.c
@@ -29,6 +29,7 @@
 // C-AARCH64-BAREMETAL: "-isysroot" 
"{{.*}}Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
 // C-AARCH64-BAREMETAL: "-internal-isystem" 
"{{.*}}Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include"
 // C-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld"
+// C-AARCH64-BAREMETAL: 
"--sysroot={{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
 // C-AARCH64-BAREMETAL: "-Bstatic" "-EL"
 // C-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o"
 // C-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
@@ -65,6 +66,7 @@
 // CXX-AARCH64-BAREMETAL: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1"
 // CXX-AARCH64-BAREMETAL: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include"
 // CXX-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld"
+// CXX-AARCH64-BAREMETAL: 
"--sysroot={{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
 // CXX-AARCH64-BAREMETAL: "-Bstatic" "-EL"
 // CXX-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o"
 // CXX-AARCH64-BAREMETAL: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
@@ -101,7 +103,8 @@
 // CXX-AARCH64-BAREMETAL-LIBCXX: "-isysroot" 
"{{.*}}Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
 // CXX-AARCH64-BAREMETAL-LIBCXX: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/v1"
 // CXX-AARCH64-BAREMETAL-LIBCXX: "-internal-isystem" 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include"
-// CXX-AARCH64-BAREMETAL-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld
+// CXX-AARCH64-BAREMETAL-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/../../../../bin/aarch64-none-elf-ld"
+// CXX-AARCH64-BAREMETAL-LIBCXX: 
"--sysroot={{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf"
 // CXX-AARCH64-BAREMETAL-LIBCXX: "-Bstatic" "-EL"
 // CXX-AARCH64-BAREMETAL-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o"
 // CXX-AARCH64-BAREMETAL-LIBCXX: 
"{{.*}}/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o"
diff --git a/clang/test/Driver/arm-toolchain.c 
b/clang/test/Driver/arm-toolchain.c
index b600efd846f0e..f564996d83ce8 100644
--- a/clang/test/Driver/arm-toolchain.c
+++ b/clang/test/Driver/arm-toolchain.c
@@ -28,6 +28,7 @@
 // C-ARM-BAREMETAL: "-isysroot" 
"{{.*}}Inputs/basic_arm_gcc_tree/armv6m-none-eabi"
 // C-ARM-BAREMETAL: "-internal-isystem" 
"{{.*}}Inputs/basic_arm_gcc_tree/armv6m-none-eabi/include"
 // C-ARM-BAREMETAL: 
"{{.*}}/Inputs/basic_arm_gcc_tree/lib/gcc/armv6m-none-eabi/8.2.1/../../../../bin/armv6m-none-eabi-ld"
+// C-ARM-BAREMETAL: 
"--sysroot={{.*}}/Inputs/basic_arm_

[clang] Remove extraneous addition of `-X` flag in baremetal toolchain (PR #148855)

2025-07-22 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt closed 
https://github.com/llvm/llvm-project/pull/148855
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Teach Barmetal toolchain about GCC installation (PR #121829)

2025-06-03 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

Hi @smithp35, can you pls review the latest changes and approve this PR if 
everything looks fine?

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Reland [Driver] Add support for GCC installation detection in Baremetal toolchain (PR #144640)

2025-06-17 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt edited 
https://github.com/llvm/llvm-project/pull/144640
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Add support for GCC installation detection in Baremetal toolchain (PR #121829)

2025-06-17 Thread Garvit Gupta via cfe-commits

quic-garvgupt wrote:

> > Do the fuchsia builders actually produce output on a failure? We seem to 
> > just get exit code 1, but no indication of what filecheck had problems with.
> 
> They do; here's a link! Let me know if you have an problems with the Web UI; 
> I can paste things here if so. 
> https://luci-milo.appspot.com/ui/p/fuchsia/builders/toolchain.ci/clang-linux-x64/b8711783358287375697/overview

The check lines that are not found in the tests were added as part of different 
commit ec230aa and not this one. I will be relanding this commit as is here 
https://github.com/llvm/llvm-project/pull/144640

https://github.com/llvm/llvm-project/pull/121829
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Add support for GCC installation detection in Baremetal toolchain (PR #144640)

2025-06-17 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/144640

This patch introduces enhancements to the Baremetal toolchain to support
GCC toolchain detection.
- If the --gcc-install-dir or --gcc-toolchain options are provided and
point to valid paths, the sysroot is derived from those locations.
- If not, the logic falls back to the existing sysroot inference
mechanism already present in the Baremetal toolchain.
- Support for adding include paths for the libstdc++ library has also
been added.

Additionally, the restriction to always use the integrated assembler has
been removed. With a valid GCC installation, the GNU assembler can now
be used as well.

This patch currently updates and adds tests for the ARM target only.
RISC-V-specific tests will be introduced in a later patch, once the
RISCVToolChain is fully merged into the Baremetal toolchain. At this
stage, there is no way to test the RISC-V target within this PR.

RFC:
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524

>From b5bf681e5d503569253eabc9c987e7d9797e53c8 Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Tue, 17 Jun 2025 21:43:17 +0530
Subject: [PATCH] [Driver] Add support for GCC installation detection in
 Baremetal toolchain (#121829)

This patch introduces enhancements to the Baremetal toolchain to support
GCC toolchain detection.
- If the --gcc-install-dir or --gcc-toolchain options are provided and
point to valid paths, the sysroot is derived from those locations.
- If not, the logic falls back to the existing sysroot inference
mechanism already present in the Baremetal toolchain.
- Support for adding include paths for the libstdc++ library has also
been added.

Additionally, the restriction to always use the integrated assembler has
been removed. With a valid GCC installation, the GNU assembler can now
be used as well.

This patch currently updates and adds tests for the ARM target only.
RISC-V-specific tests will be introduced in a later patch, once the
RISCVToolChain is fully merged into the Baremetal toolchain. At this
stage, there is no way to test the RISC-V target within this PR.

RFC:
https://discourse.llvm.org/t/merging-riscvtoolchain-and-baremetal-toolchains/75524
---
 clang/docs/Toolchain.rst  |   5 +
 .../clang/Basic/DiagnosticDriverKinds.td  |   3 +
 clang/lib/Driver/ToolChains/BareMetal.cpp | 235 +-
 clang/lib/Driver/ToolChains/BareMetal.h   |  19 +-
 .../aarch64-none-elf/include/c++/8.2.1/.keep  |   0
 .../aarch64-none-elf/lib/.keep|   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o |   0
 .../lib/gcc/aarch64-none-elf/8.2.1/crtend.o   |   0
 .../aarch64-none-elf/lib/crt0.o   |   0
 .../aarch64-none-elf/lib/crtbegin.o   |   0
 .../aarch64-none-elf/lib/crtend.o |   0
 .../bin/aarch64-none-elf-ld   |   1 +
 .../armv6m-none-eabi/include/c++/8.2.1/.keep  |   0
 .../armv6m-none-eabi/lib/.keep|   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtbegin.o |   0
 .../lib/gcc/armv6m-none-eabi/8.2.1/crtend.o   |   0
 .../armv6m-none-eabi/lib/crt0.o   |   0
 .../armv6m-none-eabi/lib/crtbegin.o   |   0
 .../armv6m-none-eabi/lib/crtend.o |   0
 .../bin/armv6m-none-eabi-ld   |   1 +
 clang/test/Driver/aarch64-gnutools.c  |   4 +
 clang/test/Driver/aarch64-toolchain-extra.c   |  28 +++
 clang/test/Driver/aarch64-toolchain.c |  61 +
 clang/test/Driver/arm-gnutools.c  |   6 +
 clang/test/Driver/arm-toolchain-extra.c   |  29 +++
 clang/test/Driver/arm-toolchain.c |  62 +
 clang/test/Driver/baremetal.cpp   |  16 ++
 clang/test/Driver/check-no-multlib-warning.c  |  10 +
 32 files changed, 418 insertions(+), 64 deletions(-)
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/include/c++/8.2.1/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/.keep
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100755 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/bin/aarch64-none-elf-ld
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_gcc_tree/lib/gcc/aarch64-none-elf/8.2.1/crtend.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crt0.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtbegin.o
 create mode 100644 
clang/test/Driver/Inputs/basic_aarch64_nogcc_tree/aarch64-none-elf/lib/crtend.o
 create mode 10

[clang] Fix tests failing on fuchsia clang x86_64 builders (PR #144655)

2025-06-18 Thread Garvit Gupta via cfe-commits

https://github.com/quic-garvgupt created 
https://github.com/llvm/llvm-project/pull/144655

Fuchsia sets CLANG_DEFAULT_UNWINDLIB to libunwind. As a result, when rtlib is 
set to libgcc and unwindlib is not explicitly specified, tests using Fuchsia as 
the default platform will fail. To address this, the affected tests are now 
marked as unsupported on the Fuchsia platform.

This change fixes the following tests introduced in 
https://github.com/llvm/llvm-project/commit/45ea46c44636094e9fcdbbeabfd11f9d0fad5e38:

clang/test/Driver/aarch64-toolchain-extra.c
clang/test/Driver/arm-toolchain-extra.c
clang/test/Driver/aarch64-toolchain.c
clang/test/Driver/arm-toolchain.c

>From 5041a43ba895f1dc49e02b894a535976671124fe Mon Sep 17 00:00:00 2001
From: Garvit Gupta 
Date: Wed, 18 Jun 2025 01:57:27 -0700
Subject: [PATCH] Fix tests failing on fuchsia clang x86_64 builders

Change-Id: I18c76810d7a58770ab4883d29c2da544730ade62
---
 clang/test/Driver/aarch64-toolchain-extra.c | 2 +-
 clang/test/Driver/aarch64-toolchain.c   | 2 +-
 clang/test/Driver/arm-toolchain-extra.c | 2 +-
 clang/test/Driver/arm-toolchain.c   | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/test/Driver/aarch64-toolchain-extra.c 
b/clang/test/Driver/aarch64-toolchain-extra.c
index 2610e962bd690..d8ba0556167ab 100644
--- a/clang/test/Driver/aarch64-toolchain-extra.c
+++ b/clang/test/Driver/aarch64-toolchain-extra.c
@@ -4,7 +4,7 @@
 // these tests need to create symlinks to test directory trees in order to
 // set up the environment and therefore shell support is required.
 // REQUIRES: shell
-// UNSUPPORTED: system-windows
+// UNSUPPORTED: system-windows, target={{.*}}-fuchsia{{.*}}
 
 // If there is no GCC install detected then the driver searches for executables
 // and runtime starting from the directory tree above the driver itself.
diff --git a/clang/test/Driver/aarch64-toolchain.c 
b/clang/test/Driver/aarch64-toolchain.c
index 7f2c01d928e43..dfa63bfff4d1c 100644
--- a/clang/test/Driver/aarch64-toolchain.c
+++ b/clang/test/Driver/aarch64-toolchain.c
@@ -1,4 +1,4 @@
-// UNSUPPORTED: system-windows
+// UNSUPPORTED: system-windows, target={{.*}}-fuchsia{{.*}}
 
 // RUN: %clang -### %s -fuse-ld= \
 // RUN:   --target=aarch64-none-elf --rtlib=libgcc \
diff --git a/clang/test/Driver/arm-toolchain-extra.c 
b/clang/test/Driver/arm-toolchain-extra.c
index 114de0a8154ab..f3e4f5f368d38 100644
--- a/clang/test/Driver/arm-toolchain-extra.c
+++ b/clang/test/Driver/arm-toolchain-extra.c
@@ -4,7 +4,7 @@
 // these tests need to create symlinks to test directory trees in order to
 // set up the environment and therefore shell support is required.
 // REQUIRES: shell
-// UNSUPPORTED: system-windows
+// UNSUPPORTED: system-windows, target={{.*}}-fuchsia{{.*}}
 
 // If there is no GCC install detected then the driver searches for executables
 // and runtime starting from the directory tree above the driver itself.
diff --git a/clang/test/Driver/arm-toolchain.c 
b/clang/test/Driver/arm-toolchain.c
index 2e38461fb7a3e..8cd6c5e96b7a0 100644
--- a/clang/test/Driver/arm-toolchain.c
+++ b/clang/test/Driver/arm-toolchain.c
@@ -1,4 +1,4 @@
-// UNSUPPORTED: system-windows
+// UNSUPPORTED: system-windows, target={{.*}}-fuchsia{{.*}}
 
 // RUN: %clang -### %s -fuse-ld= \
 // RUN:   --target=armv6m-none-eabi --rtlib=libgcc \

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


  1   2   >