[llvm] [TableGen] Use StringMap in getValueType instead of StringSwitch (PR #213229)
Manasij Mukherjee via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 31 10:06:35 PDT 2026
https://github.com/manasij7479 updated https://github.com/llvm/llvm-project/pull/213229
>From 5f7fdb25bc75ba7b6461e9e04243219bec6e7028 Mon Sep 17 00:00:00 2001
From: Manasij Mukherjee <manasijm at nvidia.com>
Date: Fri, 31 Jul 2026 08:57:47 +0000
Subject: [PATCH 1/2] [TableGen] Use StringMap in getValueType instead of
StringSwitch
StringSwitch is linear.
getValueType gets called often, identified by callgrind to be
responsible for 11% of the executed instructions for NVPTX -gen-dag-isel.
NVPTX and RISCV -gen-dag-isel become 20% and 15% faster respectively.
Modest 2-6% improvement for others.
---
llvm/utils/TableGen/Common/CodeGenTarget.cpp | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.cpp b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
index 908c4392f1352..f1be77f7e85c2 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
@@ -18,7 +18,7 @@
#include "CodeGenRegisters.h"
#include "CodeGenSchedule.h"
#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
@@ -43,12 +43,13 @@ static cl::opt<unsigned>
/// Returns the MVT that the specified TableGen
/// record corresponds to.
MVT llvm::getValueType(const Record *Rec) {
- return StringSwitch<MVT>(Rec->getValueAsString("LLVMName"))
+ static const StringMap<MVT> ValueTypes = {
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
- .Case(#Ty, MVT::Ty)
+ {#Ty, MVT::Ty},
#include "llvm/CodeGen/GenVT.inc"
#undef GET_VT_ATTR
- .Case("INVALID_SIMPLE_VALUE_TYPE", MVT::INVALID_SIMPLE_VALUE_TYPE);
+ {"INVALID_SIMPLE_VALUE_TYPE", MVT::INVALID_SIMPLE_VALUE_TYPE}};
+ return ValueTypes.lookup(Rec->getValueAsString("LLVMName"));
}
StringRef llvm::getEnumName(MVT T) {
>From 72858218d01e236e32253632db6228afc3eabc05 Mon Sep 17 00:00:00 2001
From: Manasij Mukherjee <manasijm at nvidia.com>
Date: Fri, 31 Jul 2026 17:09:03 +0000
Subject: [PATCH 2/2] Change to DenseMap
---
llvm/utils/TableGen/Common/CodeGenTarget.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/utils/TableGen/Common/CodeGenTarget.cpp b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
index f1be77f7e85c2..6965ee8c75b15 100644
--- a/llvm/utils/TableGen/Common/CodeGenTarget.cpp
+++ b/llvm/utils/TableGen/Common/CodeGenTarget.cpp
@@ -17,8 +17,8 @@
#include "CodeGenInstruction.h"
#include "CodeGenRegisters.h"
#include "CodeGenSchedule.h"
+#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
@@ -43,7 +43,7 @@ static cl::opt<unsigned>
/// Returns the MVT that the specified TableGen
/// record corresponds to.
MVT llvm::getValueType(const Record *Rec) {
- static const StringMap<MVT> ValueTypes = {
+ static const DenseMap<StringRef, MVT> ValueTypes = {
#define GET_VT_ATTR(Ty, Sz, Any, Int, FP, Vec, Sc, Tup, NF, NElem, EltTy) \
{#Ty, MVT::Ty},
#include "llvm/CodeGen/GenVT.inc"
More information about the llvm-commits
mailing list