Skip to content

Commit 1ae307a

Browse files
authored
[CIR] Add cir-translate and cir-lsp-server tools (#131181)
Adds two new clang tools to the tree. * cir-translate: Translates CIR to LLVM, similar to mlir-translate * cir-lsp-server: Implementation of the Language Server Protocol for ClangIR, similar to fir-lsp-server --------- Co-authored-by: Morris Hafner <[email protected]>
1 parent 7b3455e commit 1ae307a

12 files changed

+369
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.ll
2+
// RUN: FileCheck %s -input-file %t.ll -check-prefix=LLVM
3+
4+
module {
5+
cir.func @foo() {
6+
cir.return
7+
}
8+
}
9+
10+
// LLVM-DAG: target triple = "x86_64-unknown-linux-gnu"
11+
// LLVM-DAG: target datalayout = "{{.*}}"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll
6+
// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT
7+
8+
module attributes {
9+
cir.triple = "spirv64-unknown-unknown",
10+
dlti.dl_spec = #dlti.dl_spec<"dlti.global_memory_space" = 7 : ui64>
11+
} {
12+
cir.func @foo() {
13+
cir.return
14+
}
15+
}
16+
17+
// X86-NOT: target datalayout = "G7"
18+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
19+
20+
// SPIRV64-NOT: target datalayout = "G7"
21+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
22+
23+
// DEFAULT-DAG: target datalayout = "G7"
24+
// DEFAULT-DAG: target triple = "spirv64-unknown-unknown"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
// RUN: cir-translate --cir-to-llvmir --disable-cc-lowering %s -o %t.default.ll
6+
// RUN: FileCheck %s -input-file %t.default.ll -check-prefix=DEFAULT
7+
8+
module attributes {
9+
cir.triple = "spirv64-unknown-unknown"
10+
} {
11+
cir.func @foo() {
12+
cir.return
13+
}
14+
}
15+
16+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
17+
// X86-DAG: target datalayout = "{{.*}}"
18+
19+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
20+
// SPIRV64-DAG: target datalayout = "{{.*}}"
21+
22+
// DEFAULT-DAG: target triple = "spirv64-unknown-unknown"
23+
// DEFAULT-DAG: target datalayout = "{{.*}}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --target foobar --disable-cc-lowering %s 2>&1
2+
3+
// expected-error@below {{invalid target triple 'foobar'}}
4+
module {
5+
cir.func @foo() {
6+
cir.return
7+
}
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
6+
module attributes {
7+
dlti.dl_spec = #dlti.dl_spec<"dlti.global_memory_space" = 7 : ui64>
8+
} {
9+
cir.func @foo() {
10+
cir.return
11+
}
12+
}
13+
14+
// X86-NOT: target datalayout = "G7"
15+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
16+
17+
// SPIRV64-NOT: target datalayout = "G7"
18+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: cir-translate --cir-to-llvmir --target x86_64-unknown-linux-gnu --disable-cc-lowering %s -o %t.x86.ll
2+
// RUN: FileCheck %s -input-file %t.x86.ll -check-prefix=X86
3+
// RUN: cir-translate --cir-to-llvmir --target spirv64-unknown-unknown --disable-cc-lowering %s -o %t.spirv64.ll
4+
// RUN: FileCheck %s -input-file %t.spirv64.ll -check-prefix=SPIRV64
5+
6+
module {
7+
cir.func @foo() {
8+
cir.return
9+
}
10+
}
11+
12+
// X86-DAG: target triple = "x86_64-unknown-linux-gnu"
13+
// X86-DAG: target datalayout = "{{.*}}"
14+
15+
// SPIRV64-DAG: target triple = "spirv64-unknown-unknown"
16+
// SPIRV64-DAG: target datalayout = "{{.*}}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: cir-translate -verify-diagnostics --cir-to-llvmir --disable-cc-lowering %s
2+
3+
// expected-warning@below {{no target triple provided, assuming}}
4+
module {
5+
cir.func @foo() {
6+
cir.return
7+
}
8+
}

clang/tools/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ add_clang_subdirectory(driver)
55
add_clang_subdirectory(apinotes-test)
66
if(CLANG_ENABLE_CIR)
77
add_clang_subdirectory(cir-opt)
8+
add_clang_subdirectory(cir-translate)
9+
add_clang_subdirectory(cir-lsp-server)
810
endif()
911
add_clang_subdirectory(clang-diff)
1012
add_clang_subdirectory(clang-format)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
2+
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
3+
4+
include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
5+
include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)
6+
7+
set(LIBS
8+
${dialect_libs}
9+
${conversion_libs}
10+
${test_libs}
11+
clangCIR
12+
clangCIRLoweringDirectToLLVM
13+
MLIRCIR
14+
MLIRAffineAnalysis
15+
MLIRAnalysis
16+
MLIRDialect
17+
MLIRLspServerLib
18+
MLIRParser
19+
MLIRPass
20+
MLIRTransforms
21+
MLIRTransformUtils
22+
MLIRSupport
23+
MLIRIR
24+
)
25+
26+
add_mlir_tool(cir-lsp-server
27+
cir-lsp-server.cpp
28+
29+
DEPENDS
30+
${LIBS}
31+
)
32+
33+
target_link_libraries(cir-lsp-server PRIVATE ${LIBS})
34+
llvm_update_compile_flags(cir-lsp-server)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===----------------------------------------------------------------------===//
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+
// A language server for ClangIR
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "mlir/IR/Dialect.h"
14+
#include "mlir/IR/MLIRContext.h"
15+
#include "mlir/InitAllDialects.h"
16+
#include "mlir/Tools/mlir-lsp-server/MlirLspServerMain.h"
17+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
18+
19+
int main(int argc, char **argv) {
20+
mlir::DialectRegistry registry;
21+
mlir::registerAllDialects(registry);
22+
registry.insert<cir::CIRDialect>();
23+
return failed(mlir::MlirLspServerMain(argc, argv, registry));
24+
}

0 commit comments

Comments
 (0)