[llvm] [SandboxIR][Type] Implement ByteType (PR #197309)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 12 14:49:33 PDT 2026


https://github.com/vporpo created https://github.com/llvm/llvm-project/pull/197309

This is mirroring LLVM IR's ByteType.

>From ff0e0dd85f903499afc485076bffa0da12d22ff6 Mon Sep 17 00:00:00 2001
From: Vasileios Porpodas <vasileios.porpodas at amd.com>
Date: Tue, 12 May 2026 21:07:08 +0000
Subject: [PATCH] [SandboxIR][Type] Implement ByteType

This is mirroring LLVM IR's ByteType.
---
 llvm/include/llvm/SandboxIR/Context.h  |  1 +
 llvm/include/llvm/SandboxIR/Type.h     | 42 +++++++++++++++++++++
 llvm/lib/SandboxIR/Type.cpp            | 32 ++++++++++++++++
 llvm/unittests/SandboxIR/TypesTest.cpp | 51 ++++++++++++++++++++++++++
 4 files changed, 126 insertions(+)

diff --git a/llvm/include/llvm/SandboxIR/Context.h b/llvm/include/llvm/SandboxIR/Context.h
index ff15a9abbf626..1b8ecc86c8c3f 100644
--- a/llvm/include/llvm/SandboxIR/Context.h
+++ b/llvm/include/llvm/SandboxIR/Context.h
@@ -71,6 +71,7 @@ class Context {
   friend class Type;              // For LLVMCtx.
   friend class PointerType;       // For LLVMCtx.
   friend class IntegerType;       // For LLVMCtx.
+  friend class ByteType;          // For LLVMCtx.
   friend class StructType;        // For LLVMCtx.
   friend class Region;            // For LLVMCtx.
   friend class IRSnapshotChecker; // To snapshot LLVMModuleToModuleMap.
diff --git a/llvm/include/llvm/SandboxIR/Type.h b/llvm/include/llvm/SandboxIR/Type.h
index 9059c9661650b..8b81288266901 100644
--- a/llvm/include/llvm/SandboxIR/Type.h
+++ b/llvm/include/llvm/SandboxIR/Type.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_SANDBOXIR_TYPE_H
 #define LLVM_SANDBOXIR_TYPE_H
 
+#include "llvm/ADT/APInt.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Type.h"
@@ -25,6 +26,7 @@ namespace llvm::sandboxir {
 class Context;
 // Forward declare friend classes for MSVC.
 class ArrayType;
+class ByteType;
 class CallBase;
 class CmpInst;
 class ConstantDataSequential;
@@ -49,6 +51,7 @@ class Type {
 protected:
   llvm::Type *LLVMTy;
   friend class ArrayType;          // For LLVMTy.
+  friend class ByteType;           // For LLVMTy.
   friend class StructType;         // For LLVMTy.
   friend class VectorType;         // For LLVMTy.
   friend class FixedVectorType;    // For LLVMTy.
@@ -169,6 +172,14 @@ class Type {
   /// True if this is an instance of IntegerType.
   bool isIntegerTy() const { return LLVMTy->isIntegerTy(); }
 
+  /// True if this is an instance of ByteType.
+  bool isByteTy() const { return LLVMTy->isByteTy(); }
+
+  /// Return true if this is a ByteType of the given width.
+  bool isByteTy(unsigned Bitwidth) const {
+    return LLVMTy->isByteTy(Bitwidth);
+  }
+
   /// Return true if this is an IntegerType of the given width.
   bool isIntegerTy(unsigned Bitwidth) const {
     return LLVMTy->isIntegerTy(Bitwidth);
@@ -278,6 +289,19 @@ class Type {
   LLVM_ABI static IntegerType *getInt16Ty(Context &Ctx);
   LLVM_ABI static IntegerType *getInt8Ty(Context &Ctx);
   LLVM_ABI static IntegerType *getInt1Ty(Context &Ctx);
+  LLVM_ABI static ByteType *getByteNTy(Context &Ctx, unsigned N);
+  LLVM_ABI static ByteType *getByte1Ty(Context &Ctx);
+  LLVM_ABI static ByteType *getByte8Ty(Context &Ctx);
+  LLVM_ABI static ByteType *getByte16Ty(Context &Ctx);
+  LLVM_ABI static ByteType *getByte32Ty(Context &Ctx);
+  LLVM_ABI static ByteType *getByte64Ty(Context &Ctx);
+  LLVM_ABI static ByteType *getByte128Ty(Context &Ctx);
+  /// Returns an integer (vector of integer) type with the same size of a byte
+  /// of the given byte (vector of byte) type.
+  LLVM_ABI static Type *getIntFromByteType(Type *Ty);
+  /// Returns a byte (vector of byte) type with the same size of an integer of
+  /// the given integer (vector of integer) type.
+  LLVM_ABI static Type *getByteFromIntType(Type *Ty);
   LLVM_ABI static Type *getDoubleTy(Context &Ctx);
   LLVM_ABI static Type *getFloatTy(Context &Ctx);
   LLVM_ABI static Type *getHalfTy(Context &Ctx);
@@ -479,6 +503,24 @@ class IntegerType : public Type {
   }
 };
 
+/// Class to represent byte types.
+class ByteType : public Type {
+public:
+  LLVM_ABI static ByteType *get(Context &C, unsigned NumBits);
+
+  /// Get the number of bits in this ByteType
+  unsigned getBitWidth() const {
+    return cast<llvm::ByteType>(LLVMTy)->getBitWidth();
+  }
+
+  /// Get a bit mask for this type.
+  APInt getMask() const { return cast<llvm::ByteType>(LLVMTy)->getMask(); }
+
+  static bool classof(const Type *From) {
+    return isa<llvm::ByteType>(From->LLVMTy);
+  }
+};
+
 } // namespace llvm::sandboxir
 
 #endif // LLVM_SANDBOXIR_TYPE_H
diff --git a/llvm/lib/SandboxIR/Type.cpp b/llvm/lib/SandboxIR/Type.cpp
index 4ae678f6673e5..b24420e30f70a 100644
--- a/llvm/lib/SandboxIR/Type.cpp
+++ b/llvm/lib/SandboxIR/Type.cpp
@@ -30,6 +30,33 @@ IntegerType *Type::getInt8Ty(Context &Ctx) {
 IntegerType *Type::getInt1Ty(Context &Ctx) {
   return cast<IntegerType>(Ctx.getType(llvm::Type::getInt1Ty(Ctx.LLVMCtx)));
 }
+ByteType *Type::getByteNTy(Context &Ctx, unsigned N) {
+  return cast<ByteType>(Ctx.getType(llvm::Type::getByteNTy(Ctx.LLVMCtx, N)));
+}
+ByteType *Type::getByte1Ty(Context &Ctx) {
+  return cast<ByteType>(Ctx.getType(llvm::Type::getByte1Ty(Ctx.LLVMCtx)));
+}
+ByteType *Type::getByte8Ty(Context &Ctx) {
+  return cast<ByteType>(Ctx.getType(llvm::Type::getByte8Ty(Ctx.LLVMCtx)));
+}
+ByteType *Type::getByte16Ty(Context &Ctx) {
+  return cast<ByteType>(Ctx.getType(llvm::Type::getByte16Ty(Ctx.LLVMCtx)));
+}
+ByteType *Type::getByte32Ty(Context &Ctx) {
+  return cast<ByteType>(Ctx.getType(llvm::Type::getByte32Ty(Ctx.LLVMCtx)));
+}
+ByteType *Type::getByte64Ty(Context &Ctx) {
+  return cast<ByteType>(Ctx.getType(llvm::Type::getByte64Ty(Ctx.LLVMCtx)));
+}
+ByteType *Type::getByte128Ty(Context &Ctx) {
+  return cast<ByteType>(Ctx.getType(llvm::Type::getByte128Ty(Ctx.LLVMCtx)));
+}
+Type *Type::getIntFromByteType(Type *Ty) {
+  return Ty->getContext().getType(llvm::Type::getIntFromByteType(Ty->LLVMTy));
+}
+Type *Type::getByteFromIntType(Type *Ty) {
+  return Ty->getContext().getType(llvm::Type::getByteFromIntType(Ty->LLVMTy));
+}
 Type *Type::getDoubleTy(Context &Ctx) {
   return Ctx.getType(llvm::Type::getDoubleTy(Ctx.LLVMCtx));
 }
@@ -125,3 +152,8 @@ IntegerType *IntegerType::get(Context &Ctx, unsigned NumBits) {
   return cast<IntegerType>(
       Ctx.getType(llvm::IntegerType::get(Ctx.LLVMCtx, NumBits)));
 }
+
+ByteType *ByteType::get(Context &Ctx, unsigned NumBits) {
+  return cast<ByteType>(
+      Ctx.getType(llvm::ByteType::get(Ctx.LLVMCtx, NumBits)));
+}
diff --git a/llvm/unittests/SandboxIR/TypesTest.cpp b/llvm/unittests/SandboxIR/TypesTest.cpp
index 880caa7137a44..022ec006be3b0 100644
--- a/llvm/unittests/SandboxIR/TypesTest.cpp
+++ b/llvm/unittests/SandboxIR/TypesTest.cpp
@@ -467,3 +467,54 @@ define void @foo(i32 %v0) {
   auto *NewInt32Ty = sandboxir::IntegerType::get(Ctx, 32u);
   EXPECT_EQ(NewInt32Ty, Int32Ty);
 }
+
+TEST_F(SandboxTypeTest, ByteType) {
+  parseIR(C, R"IR(
+define void @foo() {
+  ret void
+}
+)IR");
+  sandboxir::Context Ctx(C);
+  // Check get().
+  auto *Byte8Ty = sandboxir::ByteType::get(Ctx, 8u);
+  EXPECT_TRUE(isa<sandboxir::ByteType>(Byte8Ty));
+  // Check getBitWidth().
+  EXPECT_EQ(Byte8Ty->getBitWidth(), 8u);
+  // Check getMask().
+  EXPECT_EQ(Byte8Ty->getMask(), APInt(8, 0xFF));
+  // Check classof().
+  auto *Byte16Ty = sandboxir::ByteType::get(Ctx, 16u);
+  EXPECT_TRUE(isa<sandboxir::ByteType>(Byte16Ty));
+  EXPECT_EQ(Byte16Ty->getBitWidth(), 16u);
+  EXPECT_EQ(Byte16Ty->getMask(), APInt(16, 0xFFFF));
+  // Check Type::getByteNTy().
+  EXPECT_EQ(sandboxir::Type::getByteNTy(Ctx, 8u), Byte8Ty);
+  EXPECT_EQ(sandboxir::Type::getByteNTy(Ctx, 16u), Byte16Ty);
+  // Check Type::getByte1Ty().
+  EXPECT_EQ(sandboxir::Type::getByte1Ty(Ctx)->getBitWidth(), 1u);
+  // Check Type::getByte8Ty().
+  EXPECT_EQ(sandboxir::Type::getByte8Ty(Ctx), Byte8Ty);
+  // Check Type::getByte16Ty().
+  EXPECT_EQ(sandboxir::Type::getByte16Ty(Ctx), Byte16Ty);
+  // Check Type::getByte32Ty().
+  EXPECT_EQ(sandboxir::Type::getByte32Ty(Ctx)->getBitWidth(), 32u);
+  // Check Type::getByte64Ty().
+  EXPECT_EQ(sandboxir::Type::getByte64Ty(Ctx)->getBitWidth(), 64u);
+  // Check Type::getByte128Ty().
+  EXPECT_EQ(sandboxir::Type::getByte128Ty(Ctx)->getBitWidth(), 128u);
+  // Check Type::getIntFromByteType().
+  auto *Int8Ty = sandboxir::Type::getIntFromByteType(Byte8Ty);
+  EXPECT_TRUE(isa<sandboxir::IntegerType>(Int8Ty));
+  EXPECT_EQ(Int8Ty->getScalarSizeInBits(), 8u);
+  // Check Type::getByteFromIntType().
+  auto *FromIntTy = sandboxir::Type::getByteFromIntType(Int8Ty);
+  EXPECT_TRUE(isa<sandboxir::ByteType>(FromIntTy));
+  EXPECT_EQ(FromIntTy, Byte8Ty);
+  // Check isByteTy().
+  EXPECT_TRUE(Byte8Ty->isByteTy());
+  EXPECT_FALSE(Int8Ty->isByteTy());
+  // Check isByteTy(Bitwidth).
+  EXPECT_TRUE(Byte8Ty->isByteTy(8u));
+  EXPECT_FALSE(Byte8Ty->isByteTy(16u));
+  EXPECT_FALSE(Int8Ty->isByteTy(8u));
+}



More information about the llvm-commits mailing list