Skip to content

Commit 88db308

Browse files
committed
Add LLVM compatible functions
1 parent 8eb2d72 commit 88db308

7 files changed

Lines changed: 640 additions & 3 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ build/
1111
# OS files
1212
.DS_Store
1313
Thumbs.db
14-

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ LIBDIR = $(BUILDDIR)/lib
5858
BINDIR = $(BUILDDIR)/bin
5959

6060
# Source files
61-
SOURCES = $(SRCDIR)/cpuid.cpp $(SRCDIR)/microarchitecture.cpp $(SRCDIR)/detect.cpp $(SRCDIR)/archspec_c.cpp
61+
SOURCES = $(SRCDIR)/cpuid.cpp $(SRCDIR)/microarchitecture.cpp $(SRCDIR)/detect.cpp $(SRCDIR)/archspec_c.cpp $(SRCDIR)/llvm_compat.cpp
6262
OBJECTS = $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES))
6363

6464
# Library names

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,3 @@ This library is dual-licensed under Apache-2.0 OR MIT, matching the original arc
245245

246246
- Based on [archspec](https://github.com/archspec/archspec) by Lawrence Livermore National Security, LLC
247247
- Uses [nlohmann/json](https://github.com/nlohmann/json) for JSON parsing
248-

include/archspec/archspec.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88

99
#include "microarchitecture.hpp"
1010
#include "detect.hpp"
11+
#include "llvm_compat.hpp"
1112

1213
#endif // ARCHSPEC_HPP

include/archspec/llvm_compat.hpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// LLVM feature name compatibility layer
2+
// Maps archspec feature names to LLVM feature names
3+
4+
#ifndef ARCHSPEC_LLVM_COMPAT_HPP
5+
#define ARCHSPEC_LLVM_COMPAT_HPP
6+
7+
#include <string>
8+
#include <string_view>
9+
#include <set>
10+
#include <vector>
11+
12+
namespace archspec {
13+
14+
// Forward declaration
15+
class Microarchitecture;
16+
17+
/**
18+
* Map a single archspec feature name to LLVM feature name.
19+
* Returns empty string if the feature should be filtered out (no LLVM equivalent).
20+
* Returns the input unchanged if no mapping is needed.
21+
*
22+
* @param arch_family Architecture family ("x86_64", "aarch64", "ppc64le", "riscv64")
23+
* @param feature The archspec feature name
24+
* @return LLVM-compatible feature name, or empty string to filter
25+
*/
26+
std::string map_feature_to_llvm(std::string_view arch_family, std::string_view feature);
27+
28+
/**
29+
* Get all LLVM-compatible features for a microarchitecture.
30+
* Handles mapping and filtering automatically.
31+
*
32+
* @param uarch The microarchitecture to get features for
33+
* @return Set of LLVM-compatible feature names (without +/- prefix)
34+
*/
35+
std::set<std::string> get_llvm_features(const Microarchitecture& uarch);
36+
37+
/**
38+
* Get LLVM-compatible features as a comma-separated string with +prefix.
39+
* Example: "+neon,+dotprod,+crc"
40+
*
41+
* @param uarch The microarchitecture to get features for
42+
* @return Comma-separated feature string
43+
*/
44+
std::string get_llvm_features_string(const Microarchitecture& uarch);
45+
46+
/**
47+
* Map archspec CPU name to LLVM CPU name.
48+
* Examples:
49+
* - "m4" (Apple) -> "apple-m4"
50+
* - "zen3" -> "znver3"
51+
*
52+
* @param uarch The microarchitecture
53+
* @return LLVM-compatible CPU name
54+
*/
55+
std::string get_llvm_cpu_name(const Microarchitecture& uarch);
56+
57+
/**
58+
* Normalize a CPU name to archspec format (reverse of get_llvm_cpu_name).
59+
* Examples:
60+
* - "apple-m4" -> "m4"
61+
* - "znver3" -> "zen3"
62+
*
63+
* @param arch_family Architecture family ("x86_64", "aarch64", etc.)
64+
* @param llvm_name The LLVM CPU name
65+
* @return archspec-compatible CPU name
66+
*/
67+
std::string normalize_cpu_name(std::string_view arch_family, std::string_view llvm_name);
68+
69+
/**
70+
* Look up a CPU by name (with normalization) and return its LLVM-compatible features.
71+
* Tries both the given name and normalized versions.
72+
*
73+
* @param cpu_name CPU name (can be LLVM or archspec format)
74+
* @param arch_family Architecture family for normalization
75+
* @return LLVM-compatible feature string, or empty if CPU not found
76+
*/
77+
std::string get_llvm_features_for_cpu(std::string_view cpu_name, std::string_view arch_family);
78+
79+
} // namespace archspec
80+
81+
#endif // ARCHSPEC_LLVM_COMPAT_HPP

0 commit comments

Comments
 (0)