[llvm] Add support for CHERI "purecap" as an environment in target triples. (PR #118927)

Owen Anderson via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 5 21:49:28 PST 2024


https://github.com/resistor created https://github.com/llvm/llvm-project/pull/118927

For context, CHERI architectures can support two modes:
* Hybrid mode, in which capabilities co-exist alongside normal pointers.
* Pure capability mode, in which all pointers are replaced with capabilities.

Hybrid mode does not require any indication in the target triple, as it
is treated the same as any other extension to the parent ISA. Pure cap mode,
however, is a different ABI from the native ABI of the parent architecture.


>From 4989dbcd25bf787811fea64dbf0d1bf4fa09fcde Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Fri, 6 Dec 2024 18:45:32 +1300
Subject: [PATCH] Add support for CHERI "purecap" as an environment in target
 triples.

For context, CHERI architectures can support two modes:
* Hybrid mode, in which capabilities co-exist alongside normal pointers.
* Pure capability mode, in which all pointers are replaced with capabilities.

Hybrid mode does not require any indication in the target triple, as it
is treated the same as any other extension to the parent ISA. Pure cap mode,
however, is a different ABI from the native ABI of the parent architecture.
---
 llvm/include/llvm/TargetParser/Triple.h    | 2 ++
 llvm/lib/TargetParser/Triple.cpp           | 3 +++
 llvm/unittests/TargetParser/TripleTest.cpp | 6 ++++++
 3 files changed, 11 insertions(+)

diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 3a1a962003abf5..aa82a312a0ca26 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -270,6 +270,8 @@ class Triple {
     MuslX32,
     LLVM,
 
+    CheriPureCap,
+
     MSVC,
     Itanium,
     Cygnus,
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index faabaf18d80710..013b8ae072474e 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -362,6 +362,8 @@ StringRef Triple::getEnvironmentTypeName(EnvironmentType Kind) {
     return "pauthtest";
   case LLVM:
     return "llvm";
+  case CheriPureCap:
+    return "purecap";
   }
 
   llvm_unreachable("Invalid EnvironmentType!");
@@ -743,6 +745,7 @@ static Triple::EnvironmentType parseEnvironment(StringRef EnvironmentName) {
       .StartsWith("ohos", Triple::OpenHOS)
       .StartsWith("pauthtest", Triple::PAuthTest)
       .StartsWith("llvm", Triple::LLVM)
+      .StartsWith("purecap", Triple::CheriPureCap)
       .Default(Triple::UnknownEnvironment);
 }
 
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp b/llvm/unittests/TargetParser/TripleTest.cpp
index 87f9083fa344c2..10fff4e2a96d8d 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -1347,6 +1347,12 @@ TEST(TripleTest, ParsedIDs) {
   EXPECT_EQ(Triple::Linux, T.getOS());
   EXPECT_EQ(Triple::LLVM, T.getEnvironment());
 
+  T = Triple("riscv64-unknown-linux-purecap");
+  EXPECT_EQ(Triple::riscv64, T.getArch());
+  EXPECT_EQ(Triple::UnknownVendor, T.getVendor());
+  EXPECT_EQ(Triple::Linux, T.getOS());
+  EXPECT_EQ(Triple::CheriPureCap, T.getEnvironment());
+
   T = Triple("huh");
   EXPECT_EQ(Triple::UnknownArch, T.getArch());
 }



More information about the llvm-commits mailing list