Skip to content

Commit

Permalink
Adding unix and windows targets to the same ISPC binary
Browse files Browse the repository at this point in the history
  • Loading branch information
dbabokin authored and aneshlya committed Aug 14, 2019
1 parent 7412de8 commit b11a3b2
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 252 deletions.
21 changes: 17 additions & 4 deletions bitcode2cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

parser = argparse.ArgumentParser()
parser.add_argument("src", help="Source file to process")
parser.add_argument("runtime", help="Runtime", nargs='?', default='')
parser.add_argument("--runtime", help="Runtime", nargs='?', default='')
parser.add_argument("--os", help="Target OS", default='')
parser.add_argument("--llvm_as", help="Path to LLVM assembler executable", dest="path_to_llvm_as")
args = parser.parse_known_args()
src = args[0].src
Expand Down Expand Up @@ -39,18 +40,30 @@
name = target
if args[0].runtime != '':
name += "_" + args[0].runtime + "bit"

if args[0].os == "UNIX":
target_os = "unix"
elif args[0].os == "WINDOWS":
target_os = "win"
else:
sys.stderr.write("Unknown argument for --os: " + args[0].os)
sys.exit(1)

width = 16
sys.stdout.write("extern const unsigned char builtins_bitcode_" + name + "[] = {\n")

sys.stdout.write("extern const unsigned char builtins_bitcode_" + target_os + "_" + name + "[] = {\n")

data = as_out.stdout.read()
for i in range(0, len(data), 1):
sys.stdout.write("0x%0.2X, " % ord(data[i:i+1]))
sys.stdout.write("0x%0.2X," % ord(data[i:i+1]))

if i%width == (width-1):
sys.stdout.write("\n")
else:
sys.stdout.write(" ")

sys.stdout.write("0x00 };\n\n")
sys.stdout.write("int builtins_bitcode_" + name + "_length = " + str(len(data)) + ";\n")
sys.stdout.write("int builtins_bitcode_" + target_os + "_" + name + "_length = " + str(len(data)) + ";\n")

as_out.wait()

Expand Down
7 changes: 6 additions & 1 deletion builtins/builtins.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2010-2013, Intel Corporation
Copyright (c) 2010-2019, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -62,7 +62,12 @@
#endif

#ifndef _MSC_VER
// In unistd.h we need the definition of sysconf and _SC_NPROCESSORS_ONLN used as its arguments.
// We should include unistd.h, but it doesn't really work well for cross compilation, as
// requires us to carry around unistd.h, which is not available on Windows out of the box.
#include <unistd.h>
//#define _SC_NPROCESSORS_ONLN 58
// long sysconf(int);
#endif // !_MSC_VER

#include <stdarg.h>
Expand Down
78 changes: 44 additions & 34 deletions cmake/GenerateBuiltins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,33 @@ find_program(M4_EXECUTABLE m4)
message(STATUS "M4 macro processor: " ${M4_EXECUTABLE})

if (WIN32)
set(OS_NAME "WINDOWS")
set(TARGET_OS_LIST "windows" "unix")
elseif (UNIX)
set(OS_NAME "UNIX")
set(TARGET_OS_LIST "unix")
endif()

function(ll_to_cpp llFileName bit resultFileName)
function(ll_to_cpp llFileName bit os_name resultFileName)
set(inputFilePath builtins/${llFileName}.ll)
set(includePath builtins)
string(TOUPPER ${os_name} os_name_macro)
if ("${bit}" STREQUAL "")
set(output ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/builtins-${llFileName}.cpp)
set(output ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/builtins-${llFileName}-${os_name}.cpp)
add_custom_command(
OUTPUT ${output}
COMMAND ${M4_EXECUTABLE} -I${includePath}
-DLLVM_VERSION=${LLVM_VERSION} -DBUILD_OS=${OS_NAME} ${inputFilePath}
| \"${Python3_EXECUTABLE}\" bitcode2cpp.py ${inputFilePath} --llvm_as ${LLVM_AS_EXECUTABLE}
-DLLVM_VERSION=${LLVM_VERSION} -DBUILD_OS=${os_name_macro} ${inputFilePath}
| \"${Python3_EXECUTABLE}\" bitcode2cpp.py ${inputFilePath} --os=${os_name_macro} --llvm_as ${LLVM_AS_EXECUTABLE}
> ${output}
DEPENDS ${inputFilePath}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
else ()
set(output ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/builtins-${llFileName}-${bit}bit.cpp)
set(output ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/builtins-${llFileName}-${bit}bit-${os_name}.cpp)
add_custom_command(
OUTPUT ${output}
COMMAND ${M4_EXECUTABLE} -I${includePath}
-DLLVM_VERSION=${LLVM_VERSION} -DBUILD_OS=${OS_NAME} -DRUNTIME=${bit} ${inputFilePath}
| \"${Python3_EXECUTABLE}\" bitcode2cpp.py ${inputFilePath} ${bit} --llvm_as ${LLVM_AS_EXECUTABLE}
-DLLVM_VERSION=${LLVM_VERSION} -DBUILD_OS=${os_name_macro} -DRUNTIME=${bit} ${inputFilePath}
| \"${Python3_EXECUTABLE}\" bitcode2cpp.py ${inputFilePath} --runtime=${bit} --os=${os_name_macro} --llvm_as ${LLVM_AS_EXECUTABLE}
> ${output}
DEPENDS ${inputFilePath}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Expand All @@ -75,17 +76,20 @@ function(ll_to_cpp llFileName bit resultFileName)
set_source_files_properties(${resultFileName} PROPERTIES GENERATED true)
endfunction()

function(builtin_to_cpp bit resultFileName)
function(builtin_to_cpp bit os_name resultFileName)
set(inputFilePath builtins/builtins.c)
set(output ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/builtins-c-${bit}.cpp)
set(fpic)
if (UNIX)
set(fpic -fPIC)
set(output ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/builtins-c-${bit}-${os_name}.cpp)
## note, that clang will adjust target triple to make 32 bit when -m32 is passed.
if (${os_name} STREQUAL "windows")
set(target_flags --target="x86_64-pc-win32")
else()
set(target_flags --target="x86_64-unknown-linux-gnu" -fPIC)
endif()
string(TOUPPER ${os_name} os_name_macro)
add_custom_command(
OUTPUT ${output}
COMMAND ${CLANG_EXECUTABLE} ${fpic} -m${bit} -emit-llvm -c ${inputFilePath} -o - | \"${LLVM_DIS_EXECUTABLE}\" -
| \"${Python3_EXECUTABLE}\" bitcode2cpp.py c ${bit} --llvm_as ${LLVM_AS_EXECUTABLE}
COMMAND ${CLANG_EXECUTABLE} ${target_flags} -m${bit} -emit-llvm -c ${inputFilePath} -o - | \"${LLVM_DIS_EXECUTABLE}\" -
| \"${Python3_EXECUTABLE}\" bitcode2cpp.py c --runtime=${bit} --os=${os_name_macro} --llvm_as ${LLVM_AS_EXECUTABLE}
> ${output}
DEPENDS ${inputFilePath}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Expand All @@ -95,33 +99,39 @@ function(builtin_to_cpp bit resultFileName)
endfunction()

function (generate_target_builtins resultList)
ll_to_cpp(dispatch "" output)
list(APPEND tmpList ${output})
if(MSVC)
# Group generated files inside Visual Studio
source_group("Generated Builtins" FILES ${output})
endif()
foreach (os_name ${TARGET_OS_LIST})
ll_to_cpp(dispatch "" ${os_name} output${os_name})
list(APPEND tmpList ${output${os_name}})
if(MSVC)
# Group generated files inside Visual Studio
source_group("Generated Builtins" FILES ${output}${os_name})
endif()
endforeach()
foreach (ispc_target ${ARGN})
foreach (bit 32 64)
ll_to_cpp(target-${ispc_target} ${bit} output${bit})
list(APPEND tmpList ${output${bit}})
if(MSVC)
# Group generated files inside Visual Studio
source_group("Generated Builtins" FILES ${output${bit}})
endif()
foreach (os_name ${TARGET_OS_LIST})
ll_to_cpp(target-${ispc_target} ${bit} ${os_name} output${os_name}${bit})
list(APPEND tmpList ${output${os_name}${bit}})
if(MSVC)
# Group generated files inside Visual Studio
source_group("Generated Builtins" FILES ${output${os_name}${bit}})
endif()
endforeach()
endforeach()
endforeach()
set(${resultList} ${tmpList} PARENT_SCOPE)
endfunction()

function (generate_common_builtins resultList)
foreach (bit 32 64)
builtin_to_cpp(${bit} res${bit})
list(APPEND tmpList ${res${bit}} )
if(MSVC)
# Group generated files inside Visual Studio
source_group("Generated Builtins" FILES ${res${bit}})
endif()
foreach (os_name ${TARGET_OS_LIST})
builtin_to_cpp(${bit} ${os_name} res${bit}${os_name})
list(APPEND tmpList ${res${bit}${os_name}} )
if(MSVC)
# Group generated files inside Visual Studio
source_group("Generated Builtins" FILES ${res${bit}${os_name}})
endif()
endforeach()
endforeach()
set(${resultList} ${tmpList} PARENT_SCOPE)
endfunction()
Loading

0 comments on commit b11a3b2

Please sign in to comment.