Skip to content

Commit ac1ffd3

Browse files
committed
[TargetParser] Generate the defs for RISCV CPUs using llvm-tblgen.
Rework the change to prevent build failures. NFCI. The failing code was submitted as cf7a830 and reverted via 8bd65e5. The rework in this new commit prevents failures like the following: FAILED: tools/clang/lib/Basic/CMakeFiles/obj.clangBasic.dir/Targets/RISCV.cpp.o /usr/bin/c++ [bunch of non interesting stuff] -c <path-to>/llvm-project/clang/lib/Basic/Targets/RISCV.cpp In file included from <path-to>/llvm-project/clang/lib/Basic/Targets/RISCV.cpp:19: <path-to>/llvm-project/llvm/include/llvm/TargetParser/RISCVTargetParser.h:29:10: fatal error: llvm/TargetParser/RISCVTargetParserDef.inc: No such file or directory 29 | #include "llvm/TargetParser/RISCVTargetParserDef.inc" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ These failures happen because the library LLVMTargetParser depends on RISCVTargetParserTableGen, which is a tablegen target that generates the list of CPUs in llvm/TargetParser/RISCVTargetParserDef.inc. This *.inc file is included by the public header file llvm/TargetParser/RISCVTargetParser.h. The header file llvm/TargetParser/RISCVTargetParser.h is also used in components (clangDriver and clangBasic) that link into LLVMTargetParser, but on some configurations such components might end up being built before TargetParser is ready. The fix is to make sure that clangDriver and clangBasic depend on the tablegen target RISCVTargetParserTableGen, which generates the .inc file whether or not LLVMTargetParser is ready. WRT the original patch at https://reviews.llvm.org/D137517, this commit is just adding RISCVTargetParserTableGen in the DEPENDS list of clangDriver and clangBasic.
1 parent c8ff968 commit ac1ffd3

21 files changed

+411
-245
lines changed

clang/lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ add_clang_library(clangBasic
110110

111111
DEPENDS
112112
omp_gen
113+
RISCVTargetParserTableGen
113114
)
114115

115116
target_link_libraries(clangBasic

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include "clang/Basic/MacroBuilder.h"
1616
#include "clang/Basic/TargetBuiltins.h"
1717
#include "llvm/ADT/StringSwitch.h"
18-
#include "llvm/Support/TargetParser.h"
1918
#include "llvm/Support/raw_ostream.h"
19+
#include "llvm/TargetParser/RISCVTargetParser.h"
2020
#include <optional>
2121

2222
using namespace clang;

clang/lib/Driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ add_clang_library(clangDriver
9393

9494
DEPENDS
9595
ClangDriverOptions
96+
RISCVTargetParserTableGen
9697

9798
LINK_LIBS
9899
clangBasic

clang/lib/Driver/ToolChains/Arch/RISCV.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#include "llvm/Support/Error.h"
1818
#include "llvm/Support/Host.h"
1919
#include "llvm/Support/RISCVISAInfo.h"
20-
#include "llvm/Support/TargetParser.h"
2120
#include "llvm/Support/raw_ostream.h"
21+
#include "llvm/TargetParser/RISCVTargetParser.h"
2222

2323
using namespace clang::driver;
2424
using namespace clang::driver::tools;

llvm/include/llvm/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_subdirectory(IR)
22
add_subdirectory(Support)
33
add_subdirectory(Frontend)
4+
add_subdirectory(TargetParser)
45

56
# If we're doing an out-of-tree build, copy a module map for generated
67
# header files into the build area.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(LLVM_TARGET_DEFINITIONS ${CMAKE_SOURCE_DIR}/lib/Target/RISCV/RISCV.td)
2+
tablegen(LLVM RISCVTargetParserDef.inc -gen-riscv-target-def -I ${CMAKE_SOURCE_DIR}/lib/Target/RISCV/)
3+
add_public_tablegen_target(RISCVTargetParserTableGen)
4+

llvm/include/llvm/TargetParser/RISCVTargetParser.def

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===-- RISCVTargetParser - Parser for target features ----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements a target parser to recognise hardware features
10+
// FOR RISC-V CPUS.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TARGETPARSER_RISCVTARGETPARSER_H
15+
#define LLVM_TARGETPARSER_RISCVTARGETPARSER_H
16+
17+
#include "llvm/ADT/StringRef.h"
18+
#include <vector>
19+
20+
namespace llvm {
21+
namespace RISCV {
22+
23+
// We use 64 bits as the known part in the scalable vector types.
24+
static constexpr unsigned RVVBitsPerBlock = 64;
25+
26+
enum CPUKind : unsigned {
27+
#define PROC(ENUM, NAME, FEATURES, DEFAULT_MARCH) CK_##ENUM,
28+
#define TUNE_PROC(ENUM, NAME) CK_##ENUM,
29+
#include "llvm/TargetParser/RISCVTargetParserDef.inc"
30+
};
31+
32+
enum FeatureKind : unsigned {
33+
FK_INVALID = 0,
34+
FK_NONE = 1,
35+
FK_64BIT = 1 << 2,
36+
};
37+
38+
bool checkCPUKind(CPUKind Kind, bool IsRV64);
39+
bool checkTuneCPUKind(CPUKind Kind, bool IsRV64);
40+
CPUKind parseCPUKind(StringRef CPU);
41+
CPUKind parseTuneCPUKind(StringRef CPU, bool IsRV64);
42+
StringRef getMArchFromMcpu(StringRef CPU);
43+
void fillValidCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64);
44+
void fillValidTuneCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64);
45+
bool getCPUFeaturesExceptStdExt(CPUKind Kind, std::vector<StringRef> &Features);
46+
47+
} // namespace RISCV
48+
} // namespace llvm
49+
50+
#endif

llvm/include/llvm/TargetParser/TargetParser.h

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -154,34 +154,6 @@ void fillValidArchListR600(SmallVectorImpl<StringRef> &Values);
154154
IsaVersion getIsaVersion(StringRef GPU);
155155

156156
} // namespace AMDGPU
157-
158-
namespace RISCV {
159-
160-
// We use 64 bits as the known part in the scalable vector types.
161-
static constexpr unsigned RVVBitsPerBlock = 64;
162-
163-
enum CPUKind : unsigned {
164-
#define PROC(ENUM, NAME, FEATURES, DEFAULT_MARCH) CK_##ENUM,
165-
#define TUNE_PROC(ENUM, NAME) CK_##ENUM,
166-
#include "RISCVTargetParser.def"
167-
};
168-
169-
enum FeatureKind : unsigned {
170-
FK_INVALID = 0,
171-
FK_NONE = 1,
172-
FK_64BIT = 1 << 2,
173-
};
174-
175-
bool checkCPUKind(CPUKind Kind, bool IsRV64);
176-
bool checkTuneCPUKind(CPUKind Kind, bool IsRV64);
177-
CPUKind parseCPUKind(StringRef CPU);
178-
CPUKind parseTuneCPUKind(StringRef CPU, bool IsRV64);
179-
StringRef getMArchFromMcpu(StringRef CPU);
180-
void fillValidCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64);
181-
void fillValidTuneCPUArchList(SmallVectorImpl<StringRef> &Values, bool IsRV64);
182-
bool getCPUFeaturesExceptStdExt(CPUKind Kind, std::vector<StringRef> &Features);
183-
184-
} // namespace RISCV
185157
} // namespace llvm
186158

187159
#endif

llvm/include/llvm/module.extern.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ module LLVM_Extern_IR_Attributes_Gen {}
33
module LLVM_Extern_IR_Intrinsics_Gen {}
44
module LLVM_Extern_IR_Intrinsics_Enum {}
55
module LLVM_Extern_Utils_DataTypes {}
6+
module LLVM_Extern_TargetParser_Gen {}

llvm/include/llvm/module.install.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ module LLVM_Extern_Utils_DataTypes {
2525
header "Support/DataTypes.h"
2626
export *
2727
}
28+
29+
module LLVM_Extern_TargetParser_Gen {
30+
textual header "TargetParser/RISCVTargetParserDef.inc"
31+
}

llvm/include/llvm/module.modulemap

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ module LLVM_Transforms {
391391

392392
extern module LLVM_Extern_Utils_DataTypes "module.extern.modulemap"
393393

394+
// Build the module with the tablegen-generated files needed by the
395+
// TargetParser module before building the TargetParser module itself.
396+
module TargetParserGen {
397+
module RISCVTargetParserDef {
398+
header "TargetParser/RISCVTargetParser.h"
399+
extern module LLVM_Extern_TargetParser_Gen "module.extern.modulemap"
400+
export *
401+
}
402+
}
403+
394404
// A module covering ADT/ and Support/. These are intertwined and
395405
// codependent, and notionally form a single module.
396406
module LLVM_Utils {
@@ -427,7 +437,6 @@ module LLVM_Utils {
427437
textual header "TargetParser/AArch64TargetParser.def"
428438
textual header "TargetParser/ARMTargetParser.def"
429439
textual header "TargetParser/CSKYTargetParser.def"
430-
textual header "TargetParser/RISCVTargetParser.def"
431440
textual header "TargetParser/X86TargetParser.def"
432441
textual header "TargetParser/LoongArchTargetParser.def"
433442
}

0 commit comments

Comments
 (0)