[llvm] [CodeGen] Compact cost table entries (PR #202836)

David Zbarsky via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 5 16:46:22 PDT 2026


https://github.com/dzbarsky updated https://github.com/llvm/llvm-project/pull/202836

>From 16170b78ece6eeaa99f928b2977d9772109a9b06 Mon Sep 17 00:00:00 2001
From: David Zbarsky <dzbarsky at gmail.com>
Date: Tue, 9 Jun 2026 22:59:44 -0400
Subject: [PATCH] [CodeGen] Compact cost table entries

Store cost table operation numbers and common cost values in uint16_t fields while retaining MVT::SimpleValueType for MVT fields. Cost tables use aggregate initialization, so out-of-range operation and cost values are rejected as narrowing conversions. Keep the lookups as direct scalar comparisons and add layout assertions for the common cost entry types.

Across the X86, AArch64, ARM, RISCV, and WebAssembly target transform objects, this removes 14,072 bytes, including 13,920 bytes from __TEXT,__const. A Darwin AArch64 Release opt configured with those five targets shrinks by 16,624 bytes, with __TEXT down 16,384 bytes, __TEXT,__const down 13,936 bytes, __DATA_CONST unchanged, and chained fixups unchanged.

All five direct consumers compile, all 354 AArch64, ARM, RISCV, WebAssembly, and X86 cost-model tests pass, a valid MVT::LAST_VALUETYPE initializer compiles, and explicit oversized operation and cost initializers fail compilation.
---
 llvm/include/llvm/CodeGen/CostTable.h | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/CostTable.h b/llvm/include/llvm/CodeGen/CostTable.h
index 490de79d18997..87ab8cbe355d1 100644
--- a/llvm/include/llvm/CodeGen/CostTable.h
+++ b/llvm/include/llvm/CodeGen/CostTable.h
@@ -17,17 +17,20 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/CodeGenTypes/MachineValueType.h"
+#include <cstdint>
 
 namespace llvm {
 
 /// Cost Table Entry
-template <typename CostType>
-struct CostTblEntryT {
-  int ISD;
+template <typename CostType> struct CostTblEntryT {
+  // Cost tables use aggregate initialization, so values that do not fit in
+  // these fields are rejected as narrowing conversions at compile time.
+  uint16_t ISD;
   MVT::SimpleValueType Type;
   CostType Cost;
 };
-using CostTblEntry = CostTblEntryT<unsigned>;
+using CostTblEntry = CostTblEntryT<uint16_t>;
+static_assert(sizeof(CostTblEntry) == 6);
 
 /// Find in cost table.
 template <class CostType>
@@ -51,14 +54,14 @@ CostTableLookup(const CostTblEntryT<CostType> (&Table)[N], int ISD, MVT Ty) {
 }
 
 /// Type Conversion Cost Table
-template <typename CostType>
-struct TypeConversionCostTblEntryT {
-  int ISD;
+template <typename CostType> struct TypeConversionCostTblEntryT {
+  uint16_t ISD;
   MVT::SimpleValueType Dst;
   MVT::SimpleValueType Src;
   CostType Cost;
 };
-using TypeConversionCostTblEntry = TypeConversionCostTblEntryT<unsigned>;
+using TypeConversionCostTblEntry = TypeConversionCostTblEntryT<uint16_t>;
+static_assert(sizeof(TypeConversionCostTblEntry) == 8);
 
 /// Find in type conversion cost table.
 template <class CostType>



More information about the llvm-commits mailing list