diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -6,7 +6,7 @@ function(llvm_update_compile_flags name) get_property(sources TARGET ${name} PROPERTY SOURCES) - if("${sources}" MATCHES "\\.c(;|$)") + if("${sources}" MATCHES "\\.(c|swift)(;|$)") set(update_src_props ON) endif() @@ -66,19 +66,20 @@ if("${suf}" STREQUAL ".cpp") set_property(SOURCE ${fn} APPEND_STRING PROPERTY COMPILE_FLAGS "${target_compile_flags}") + set_property(SOURCE ${fn} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS}) endif() if("${suf}" STREQUAL ".c") set_property(SOURCE ${fn} APPEND_STRING PROPERTY COMPILE_FLAGS "${target_compile_cflags}") + set_property(SOURCE ${fn} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS}) endif() endforeach() else() # Update target props, since all sources are C++. set_property(TARGET ${name} APPEND_STRING PROPERTY COMPILE_FLAGS "${target_compile_flags}") + set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS}) endif() - - set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LLVM_COMPILE_DEFINITIONS}) endfunction() function(add_llvm_symbol_exports target_name export_file) diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -935,7 +935,7 @@ # Limit to clang and gcc so far. Add compilers supporting this option. if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - add_compile_options(-gsplit-dwarf) + add_compile_options($<$:-gsplit-dwarf>) include(LLVMCheckLinkerFlag) llvm_check_linker_flag(CXX "-Wl,--gdb-index" LINKER_SUPPORTS_GDB_INDEX) append_if(LINKER_SUPPORTS_GDB_INDEX "-Wl,--gdb-index" diff --git a/mlir/CMakeLists.txt b/mlir/CMakeLists.txt --- a/mlir/CMakeLists.txt +++ b/mlir/CMakeLists.txt @@ -90,7 +90,7 @@ set(MLIR_ENABLE_CUDA_CONVERSIONS 0) endif() # TODO: we should use a config.h file like LLVM does -add_definitions(-DMLIR_CUDA_CONVERSIONS_ENABLED=${MLIR_ENABLE_CUDA_CONVERSIONS}) +add_compile_definitions($<$:MLIR_CUDA_CONVERSIONS_ENABLED=${MLIR_ENABLE_CUDA_CONVERSIONS}>) # Build the ROCm conversions and run according tests if the AMDGPU backend # is available @@ -99,7 +99,7 @@ else() set(MLIR_ENABLE_ROCM_CONVERSIONS 0) endif() -add_definitions(-DMLIR_ROCM_CONVERSIONS_ENABLED=${MLIR_ENABLE_ROCM_CONVERSIONS}) +add_compile_definitions($<$:MLIR_ROCM_CONVERSIONS_ENABLED=${MLIR_ENABLE_ROCM_CONVERSIONS}>) set(MLIR_ENABLE_CUDA_RUNNER 0 CACHE BOOL "Enable building the mlir CUDA runner") set(MLIR_ENABLE_ROCM_RUNNER 0 CACHE BOOL "Enable building the mlir ROCm runner") @@ -121,6 +121,19 @@ set(MLIR_BUILD_MLIR_C_DYLIB 0 CACHE BOOL "Builds libMLIR-C shared library.") +#------------------------------------------------------------------------------- +# Swift Bindings Configuration +# Requires: +# Swift +#------------------------------------------------------------------------------- + +set(MLIR_ENABLE_BINDINGS_SWIFT 0 CACHE BOOL + "Enables building of Swift bindings.") +if(MLIR_ENABLE_BINDINGS_SWIFT) + enable_language(Swift) + set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift) +endif() + #------------------------------------------------------------------------------- # Python Bindings Configuration # Requires: diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -1,6 +1,10 @@ add_subdirectory(CAPI) add_subdirectory(lib) +if (MLIR_ENABLE_BINDINGS_SWIFT) + add_subdirectory(swift) +endif() + if (MLIR_ENABLE_BINDINGS_PYTHON) add_subdirectory(python) endif() @@ -56,6 +60,7 @@ llvm_canonicalize_cmake_booleans( LLVM_BUILD_EXAMPLES + MLIR_ENABLE_BINDINGS_SWIFT MLIR_ENABLE_BINDINGS_PYTHON MLIR_ENABLE_CUDA_CONVERSIONS MLIR_ENABLE_CUDA_RUNNER @@ -155,6 +160,12 @@ ) endif() +if(MLIR_ENABLE_BINDINGS_SWIFT) + list(APPEND MLIR_TEST_DEPENDS + mlir-swift-ir-test + ) +endif() + if(MLIR_ENABLE_BINDINGS_PYTHON) list(APPEND MLIR_TEST_DEPENDS MLIRPythonModules diff --git a/mlir/test/lit.cfg.py b/mlir/test/lit.cfg.py --- a/mlir/test/lit.cfg.py +++ b/mlir/test/lit.cfg.py @@ -69,6 +69,7 @@ 'mlir-capi-sparse-tensor-test', 'mlir-capi-quant-test', 'mlir-capi-pdl-test', + 'mlir-swift-ir-test', 'mlir-cpu-runner', 'mlir-linalg-ods-yaml-gen', 'mlir-reduce', diff --git a/mlir/test/lit.site.cfg.py.in b/mlir/test/lit.site.cfg.py.in --- a/mlir/test/lit.site.cfg.py.in +++ b/mlir/test/lit.site.cfg.py.in @@ -48,6 +48,7 @@ config.enable_spirv_cpu_runner = @MLIR_ENABLE_SPIRV_CPU_RUNNER@ config.vulkan_wrapper_library_dir = "@MLIR_VULKAN_WRAPPER_LIBRARY_DIR@" config.enable_vulkan_runner = @MLIR_ENABLE_VULKAN_RUNNER@ +config.enable_bindings_swift = @MLIR_ENABLE_BINDINGS_SWIFT@ config.enable_bindings_python = @MLIR_ENABLE_BINDINGS_PYTHON@ config.mlir_integration_test_dir = "@MLIR_INTEGRATION_TEST_DIR@" config.intel_sde_executable = "@INTEL_SDE_EXECUTABLE@" diff --git a/mlir/test/swift/CMakeLists.txt b/mlir/test/swift/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/mlir/test/swift/CMakeLists.txt @@ -0,0 +1,31 @@ +function(_add_swift_test_executable name) + cmake_parse_arguments(ARG + "" + "" + "LINK_LIBS" + ${ARGN}) + set(LLVM_LINK_COMPONENTS + ) + add_llvm_executable(${name} + PARTIAL_SOURCES_INTENDED + ${ARG_UNPARSED_ARGUMENTS}) + llvm_update_compile_flags(${name}) + target_compile_options(${name} + PRIVATE + -Xfrontend -enable-cxx-interop) + target_link_libraries(${name} PRIVATE + MLIRIR) + if(MLIR_BUILD_MLIR_C_DYLIB) + target_link_libraries(${name} PRIVATE + MLIR-C) + else() + target_link_libraries(${name} PRIVATE + ${ARG_LINK_LIBS}) + endif() +endfunction(_add_swift_test_executable) + +_add_swift_test_executable(mlir-swift-ir-test + ir.swift + LINK_LIBS PRIVATE + MLIRIR +) diff --git a/mlir/test/swift/ir.swift b/mlir/test/swift/ir.swift new file mode 100644 --- /dev/null +++ b/mlir/test/swift/ir.swift @@ -0,0 +1,16 @@ +//===- ir.swift - Simple test of Swift APIs -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM +// Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +/* RUN: mlir-swift-ir-test 2>&1 | FileCheck %s + */ + +import MLIRIR + +// CHECK-LABEL: @swift +print("@swift") diff --git a/mlir/test/swift/lit.local.cfg b/mlir/test/swift/lit.local.cfg new file mode 100644 --- /dev/null +++ b/mlir/test/swift/lit.local.cfg @@ -0,0 +1,4 @@ +config.suffixes.add('.swift') + +if not config.enable_bindings_swift: + config.unsupported = True