[mlir] Fix build & test of mlir python bindings on Windows

There are a couple of issues with the python bindings on Windows:
- `create_symlink` requires special permissions on Windows - using `copy_if_different` instead allows the build to complete and then be usable
- the path to the `python_executable` is likely to contain spaces if python is installed in Program Files. llvm's python substitution adds extra quotes in order to account for this case, but mlir's own python substitution does not
- the location of the shared libraries is different on windows
- if the type is not specified for numpy arrays, they appear to be treated as strings

I've implemented the smallest possible changes for each of these in the patch, but I would actually prefer a slightly more comprehensive fix for the python_executable and the shared libraries.

For the python substitution, I think it makes sense to leverage the existing %python instead of adding %PYTHON and instead add a new variable for the case when preloading is needed. This would also make it clearer which tests are which and should be skipped on platforms where the preloading won't work.

For the shared libraries, I think it would make sense to pass the correct path and extension (possibly even the names) to the python script since these are known by lit and don't have to be hardcoded in the test at all.

Reviewed By: stellaraccident

Differential Revision: https://reviews.llvm.org/D125122
This commit is contained in:
Stella Stamenova 2022-05-09 11:10:17 -07:00
parent a49d5e976e
commit 057863a9bc
4 changed files with 42 additions and 17 deletions

View file

@ -229,12 +229,20 @@ function(add_mlir_python_modules name)
get_filename_component(_install_path "${ARG_INSTALL_PREFIX}/${_dest_relative_path}" DIRECTORY)
file(MAKE_DIRECTORY "${_dest_dir}")
# On Windows create_symlink requires special permissions. Use copy_if_different instead.
if(CMAKE_HOST_WIN32)
set(_link_or_copy copy_if_different)
else()
set(_link_or_copy create_symlink)
endif()
add_custom_command(
TARGET ${modules_target} PRE_BUILD
COMMENT "Copying python source ${_src_path} -> ${_dest_path}"
DEPENDS "${_src_path}"
BYPRODUCTS "${_dest_path}"
COMMAND "${CMAKE_COMMAND}" -E create_symlink
COMMAND "${CMAKE_COMMAND}" -E ${_link_or_copy}
"${_src_path}" "${_dest_path}"
)
install(
@ -527,12 +535,9 @@ function(add_mlir_python_extension libname extname)
)
endif()
# Python extensions depends *only* on the public API and LLVMSupport unless
# if further dependencies are added explicitly.
target_link_libraries(${libname}
PRIVATE
${ARG_LINK_LIBS}
${PYEXT_LIBADD}
)
target_link_options(${libname}

View file

@ -94,6 +94,10 @@ python_executable = config.python_executable
# TODO: detect Darwin/Windows situation (or mark these tests as unsupported on these platforms).
if "asan" in config.available_features and "Linux" in config.host_os:
python_executable = f"LD_PRELOAD=$({config.host_cxx} -print-file-name=libclang_rt.asan-{config.host_arch}.so) {config.python_executable}"
# On Windows the path to python could contains spaces in which case it needs to be provided in quotes.
# This is the equivalent of how %python is setup in llvm/utils/lit/lit/llvm/config.py.
elif "Windows" in config.host_os:
python_executable = '"%s"' % (python_executable)
tools.extend([
ToolSubst('%PYTHON', python_executable, unresolved='ignore'),
])

View file

@ -23,7 +23,7 @@ def testConstShape():
RankedTensorType.get((12, -1), f32))
def const_shape_tensor(arg):
return shape.ConstShapeOp(
DenseElementsAttr.get(np.array([10, 20]), type=IndexType.get()))
DenseElementsAttr.get(np.array([10, 20], dtype=np.int64), type=IndexType.get()))
# CHECK-LABEL: func @const_shape_tensor(%arg0: tensor<12x?xf32>)
# CHECK: shape.const_shape [10, 20] : tensor<2xindex>

View file

@ -345,13 +345,21 @@ def testSharedLibLoad():
arg0_memref_ptr = ctypes.pointer(
ctypes.pointer(get_ranked_memref_descriptor(arg0)))
execution_engine = ExecutionEngine(
lowerToLLVM(module),
opt_level=3,
if sys.platform == 'win32':
shared_libs = [
"../../../../bin/mlir_runner_utils.dll",
"../../../../bin/mlir_c_runner_utils.dll"
]
else:
shared_libs = [
"../../../../lib/libmlir_runner_utils.so",
"../../../../lib/libmlir_c_runner_utils.so"
])
]
execution_engine = ExecutionEngine(
lowerToLLVM(module),
opt_level=3,
shared_libs=shared_libs)
execution_engine.invoke("main", arg0_memref_ptr)
# CHECK: Unranked Memref
# CHECK-NEXT: [42]
@ -379,13 +387,21 @@ def testNanoTime():
func.func private @printMemrefI64(memref<*xi64>) attributes { llvm.emit_c_interface }
}""")
execution_engine = ExecutionEngine(
lowerToLLVM(module),
opt_level=3,
if sys.platform == 'win32':
shared_libs = [
"../../../../bin/mlir_runner_utils.dll",
"../../../../bin/mlir_c_runner_utils.dll"
]
else:
shared_libs = [
"../../../../lib/libmlir_runner_utils.so",
"../../../../lib/libmlir_c_runner_utils.so"
])
]
execution_engine = ExecutionEngine(
lowerToLLVM(module),
opt_level=3,
shared_libs=shared_libs)
execution_engine.invoke("main")
# CHECK: Unranked Memref
# CHECK: [{{.*}}]