CMakeLists.txt 46.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#===================== begin_copyright_notice ==================================

#Copyright (c) 2017 Intel Corporation

#Permission is hereby granted, free of charge, to any person obtaining a
#copy of this software and associated documentation files (the
#"Software"), to deal in the Software without restriction, including
#without limitation the rights to use, copy, modify, merge, publish,
#distribute, sublicense, and/or sell copies of the Software, and to
#permit persons to whom the Software is furnished to do so, subject to
#the following conditions:

#The above copyright notice and this permission notice shall be included
#in all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
#CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
#TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


#======================= end_copyright_notice ==================================


# ======================================================================================================
# ================================================ UTILS ===============================================
# ======================================================================================================

# =============================================== Tools ================================================

34 35 36 37 38 39
# In new Clang versions VME types are built-in. Keep this flag until all OS's update Clang version
set(VME_TYPES_DEFINED FALSE)
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
    set(VME_TYPES_DEFINED TRUE)
endif()

40 41 42 43 44 45 46
# Decompress Clang
  # Add bogus targets to identify the .7z file and the decompressed .exe file
  add_library(CLANG_7Z        UNKNOWN IMPORTED)
  add_library(CLANG_7Z_OUTPUT UNKNOWN IMPORTED)
  add_executable(IGC_TARGET__TOOLS_CLANG    IMPORTED)

  # Detect CPU architecture
47
  igc_arch_get_cpu(_cpuSuffix)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  if(NOT _cpuSuffix MATCHES "^32$|^64$")
    message(STATUS "Unknown host CPU architecture ('" ${_cpuSuffix} "'), defaulting to using 64-bit Clang")
    set(_cpuSuffix "64")
  endif()

  # Silly detect linux distribution
  if(DEFINED "LINUX_DISTRO" AND (NOT (LINUX_DISTRO MATCHES "^$")))
    set(_osDirSuffix "${LINUX_DISTRO}")
  else()
    set(_osDirSuffix "ubuntu")
  endif()

  igc_arch_get_os(_archOs "${IGC_OPTION__ARCHITECTURE_TARGET}")
  if(_archOs MATCHES "^Windows$")
    # Use the same name
    set(_OS_folder "Windows")
  elseif(_archOs MATCHES "^Linux$")
    # Process TBD
Junjie Gu's avatar
Junjie Gu committed
66 67 68 69 70 71 72 73 74 75 76 77
    set(_OS_folder "")
    foreach(_clang_prebuilt_name "${_osDirSuffix}" "common" "ubuntu")
      set(dir_test "${IGC_BUILD__GFX_DEV_SRC_DIR}/Clang/Prebuilt/linux-${_clang_prebuilt_name}/Release/${_cpuSuffix}/clang.7z")
      if(EXISTS "${dir_test}")
        set(_OS_folder "linux-${_clang_prebuilt_name}")
        break()
      endif()
    endforeach()
    if("${_OS_folder}" STREQUAL "")
      # Prebuilt files not found.  Continue in case can be built below.
      set(_OS_folder "linux-${_osDirSuffix}")
    endif()
78 79 80 81 82 83 84 85
  elseif(_archOs MATCHES "^Android$")
    # Use Ubuntu package
    set(_OS_folder "linux-ubuntu")
  else()
    message(FATAL_ERROR "Unsupported host OS detected: " ${_archOs})
  endif()
  unset(_archOs)

86 87
  if(NOT EXISTS ${CCLANG_DIR_BINARIES})
    set(CCLANG_DIR_BINARIES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../cclang-prebuilt/${_OS_folder}")
88
  endif()  
89 90 91 92
  if(NOT EXISTS ${CCLANG_DIR_BINARIES})
    set(CCLANG_DIR_BINARIES "${IGC_BUILD__GFX_DEV_SRC_DIR}/Clang/Prebuilt/${_OS_folder}")
  endif()

93
  set(IGC_TARGET__TOOLS_CLANG_DIR "${IGC_OPTION__OUTPUT_DIR}/clang/${_OS_folder}${_cpuSuffix}")
sys_hudsonse's avatar
sys_hudsonse committed
94
  foreach(configName ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE} "Release")
95 96 97
    string(TOUPPER "${configName}" upperConfigName)
    set(locPropName "IMPORTED_LOCATION_${upperConfigName}")
    set_property(TARGET CLANG_7Z
98
        PROPERTY "${locPropName}" "${CCLANG_DIR_BINARIES}/Release/${_cpuSuffix}/clang.7z"
99 100
    )
    set_property(TARGET CLANG_7Z_OUTPUT
101
        PROPERTY "${locPropName}" "${IGC_TARGET__TOOLS_CLANG_DIR}"
102 103
    )
    set_property(TARGET IGC_TARGET__TOOLS_CLANG
104
        PROPERTY "${locPropName}" "${IGC_TARGET__TOOLS_CLANG_DIR}/clang${CMAKE_EXECUTABLE_SUFFIX}"
105 106 107 108 109 110 111 112 113 114 115 116 117
    )
    unset(locPropName)
  endforeach()

  # Get command line for extracting the clang executable
  igc_tool_get_activity_commandline(
    _extractCmd
    "IGC_TARGET__TOOLS_7Z"
    Extract
    "$<TARGET_FILE:CLANG_7Z>"
    "$<TARGET_FILE:CLANG_7Z_OUTPUT>"
  )

118 119
  get_property(_clang7zLocation TARGET CLANG_7Z PROPERTY IMPORTED_LOCATION_RELEASE)  
  
pjurek's avatar
pjurek committed
120
  if(NOT DEFINED COMMON_CLANG_LIBRARY_NAME)
121 122 123 124 125
    if(EXISTS ${_clang7zLocation})
      set(COMMON_CLANG_LIBRARY_NAME common_clang)
    else()
      set(COMMON_CLANG_LIBRARY_NAME opencl_clang)
    endif()
pjurek's avatar
pjurek committed
126 127
  endif()

128
  if(WIN32)
pjurek's avatar
pjurek committed
129
    set(COMMON_CLANG_LIB_FULL_NAME "${COMMON_CLANG_LIBRARY_NAME}${_cpuSuffix}${CMAKE_SHARED_LIBRARY_SUFFIX}")
130
  else()
pjurek's avatar
pjurek committed
131
    set(COMMON_CLANG_LIB_FULL_NAME "lib${COMMON_CLANG_LIBRARY_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX}")
132 133 134 135 136
  endif()
  
  # Add step to preform the extraction
  add_custom_command(
    OUTPUT ${IGC_TARGET__TOOLS_CLANG_DIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
137
    OUTPUT ${IGC_TARGET__TOOLS_CLANG_DIR}/${COMMON_CLANG_LIB_FULL_NAME}
138 139 140
    COMMAND "${CMAKE_COMMAND}" -E make_directory "${IGC_TARGET__TOOLS_CLANG_DIR}"
    COMMAND ${_extractCmd}
    COMMAND "${CMAKE_COMMAND}" -E touch_nocreate "${IGC_TARGET__TOOLS_CLANG_DIR}/clang${CMAKE_EXECUTABLE_SUFFIX}"
141
    COMMAND "${CMAKE_COMMAND}" -E touch_nocreate "${IGC_TARGET__TOOLS_CLANG_DIR}/${COMMON_CLANG_LIB_FULL_NAME}"
142 143 144 145 146
    DEPENDS CLANG_7Z
    COMMENT "Extracting Clang packages using: ${_extractCmd}"
  )

  add_custom_target(UnzipClang
147
    DEPENDS ${IGC_TARGET__TOOLS_CLANG_DIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
148 149 150 151 152 153 154
  )

  set_property(TARGET UnzipClang PROPERTY PROJECT_LABEL UnzipClang)

  add_custom_target(GetClang)
  add_library(common_clang_dll UNKNOWN IMPORTED GLOBAL)

155
  if(EXISTS ${_clang7zLocation})
156 157 158 159 160
    # Use precompiled common clang bundle.
    message(STATUS "Use precompiled common clang from ${_clang7zLocation}")

    add_dependencies(GetClang UnzipClang)

pjurek's avatar
pjurek committed
161
    set_property(TARGET common_clang_dll PROPERTY "IMPORTED_LOCATION" "${IGC_TARGET__TOOLS_CLANG_DIR}/${COMMON_CLANG_LIB_FULL_NAME}")
162 163
  else()
    # Do not use precompiled common clang.
164
    message(STATUS "Precompiled common clang bundle ${_clang7zLocation} does not exist. Try to compile from sources.")
165
    set(VME_TYPES_DEFINED FALSE)
166 167 168 169 170 171 172
    # if not specified from outside
    # try to use common clang component from one of fixed locations
    if(NOT IGC_CCLANG_HOME_DIR)
      set(IGC_CCLANG_HOME_DIR ${IGC_BUILD__GFX_DEV_SRC_DIR}/external/common_clang)
      if(NOT EXISTS ${IGC_CCLANG_HOME_DIR})
        set(IGC_CCLANG_HOME_DIR ${IGC_BUILD__GFX_DEV_SRC_DIR}/../common_clang)
      endif(NOT EXISTS ${IGC_CCLANG_HOME_DIR})
sys_igc's avatar
sys_igc committed
173 174 175 176
      if(EXISTS ${CCLANG_SRC})
        set(IGC_CCLANG_HOME_DIR ${CCLANG_SRC})
		message(STATUS "Setup common clang source path from buildplan argument")
      endif(NOT EXISTS ${IGC_CCLANG_HOME_DIR})
177 178 179 180 181 182 183 184 185
    endif(NOT IGC_CCLANG_HOME_DIR)
    get_filename_component(IGC_CCLANG_HOME_DIR "${IGC_CCLANG_HOME_DIR}" ABSOLUTE)
    # Set build directory
    set(IGC_CCLANG_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR}/clang_build)
    if(EXISTS ${IGC_CCLANG_HOME_DIR})
      set(CCLANG_INSTALL_ARTIFACTS_DIR ${IGC_TARGET__TOOLS_CLANG_DIR})
      message(STATUS "Trigger common clang compilation from ${IGC_CCLANG_HOME_DIR} to ${CCLANG_INSTALL_ARTIFACTS_DIR}")

      if(NOT OCL_HEADERS_DIR)
186
        get_filename_component(OCL_HEADERS_DIR "${IGC_BUILD__GFX_DEV_SRC_DIR}/third_party/opencl_headers" ABSOLUTE)
187 188 189 190
      endif()
      add_subdirectory(${IGC_CCLANG_HOME_DIR} ${IGC_CCLANG_BUILD_DIR})
      add_dependencies(GetClang "${CCLANG_LIB_NAME}")

pjurek's avatar
pjurek committed
191
      set_property(TARGET common_clang_dll PROPERTY "IMPORTED_LOCATION" "${IGC_CCLANG_BUILD_DIR}/install/lib/${COMMON_CLANG_LIB_FULL_NAME}")
192 193 194 195 196
    else()
      message(FATAL_ERROR "Can not compile common clang: unknown location of common clang sources")
    endif()
  endif()
  add_dependencies(common_clang_dll GetClang)
197

198 199 200
  if(UNIX)
    install(FILES $<TARGET_FILE:common_clang_dll> DESTINATION ${IGC_INSTALL_TIME_ROOT_DIR}/lib COMPONENT igc-opencl)
  endif(UNIX)
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
# End Clang section


# ========================================= Helper functions ===========================================

# Creates custom commands which build precompiled header for built-in functions.
#
# igc_bif_build_pch(
#     OUTPUT    outPchFilePath
#     TRIPLE    archTriple
#     HEADERS   srcHeaderFilePath [srcHeaderFilePath [...]]
#     [DEFINES  define [define [...]]]
#     [CHAINED  chainedPchFilePath]
#     [OPTIONS  [option [option [...]]]]
#   )
#
# @param outPchFilePath     Full path where output precompiled header should be written.
#                           Output file is only updated if it does not exist or it was changed.
# @param archTriple         Identifier for target architecture for compiled header.
# @param srcHeaderFilePath  Path to source header which will be precompiled (multiple can be specified).
# @param define             Additional preprocessor definition (multiple can be specified).
# @param chainedPchFilePath Path to precompiled header which will be chained with currently created
#                           precompiled header.
# @param option             Additional compilation option (multiple can be specified).
function(igc_bif_build_pch)
  set(_defines)
  set(_chainedPchFilePath)
  set(_chainedPchFilePathSet NO)
  set(_options)

  set(_parseState 0)
  foreach(_compileArg ${ARGN})
    string(REPLACE ";" "\;" _compileArg "${_compileArg}")

    # States: [0] "OUTPUT" [1] <param> [2] "TRIPLE" [3] <param> [4] "HEADERS" [5] <param> [6] *<param> [6]
    #         *1( "DEFINES" [7] <param> [8] *<param> [8] ) *1( "CHAINED" [9] <param> [10] ) *1( "OPTIONS" [11] *<param> [11] )
    # Transitions: 0 (OUTPUT) -> 1 -> 2 (TRIPLE) -> 3 -> 4 (HEADERS) -> 5 -> 6
    #              6 (DEFINES) -> 7 -> 8
    #              6 (CHAINED) -> 9 -> 10
    #              6 (OPTIONS) -> 11
    #              6 -> 6
    #              8 (CHAINED) -> 9 -> 10
    #              8 (OPTIONS) -> 11
    #              8 -> 8
    #              10 (OPTIONS) -> 11
    #              11 -> 11
    # Stop States: 6, 8, 10, 11
    if((_parseState EQUAL 0) AND (_compileArg MATCHES "^OUTPUT$"))
      set(_parseState 1)
    elseif(_parseState EQUAL 1)
      set(_outPchFilePath "${_compileArg}")
      set(_parseState 2)
    elseif((_parseState EQUAL 2) AND (_compileArg MATCHES "^TRIPLE$"))
      set(_parseState 3)
    elseif(_parseState EQUAL 3)
      set(_archTriple "${_compileArg}")
      set(_parseState 4)
    elseif((_parseState EQUAL 4) AND (_compileArg MATCHES "^HEADERS$"))
      set(_parseState 5)
    elseif(_parseState EQUAL 5)
      set(_srcHeaderFilePaths "${_compileArg}")
      set(_parseState 6)
    elseif(_parseState EQUAL 6)
      if(_compileArg MATCHES "^DEFINES$")
        set(_parseState 7)
      elseif(_compileArg MATCHES "^CHAINED$")
        set(_parseState 9)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 11)
      else()
        list(APPEND _srcHeaderFilePaths "${_compileArg}")
      endif()
    elseif(_parseState EQUAL 7)
      set(_defines "${_compileArg}")
      set(_parseState 8)
    elseif(_parseState EQUAL 8)
      if(_compileArg MATCHES "^CHAINED$")
        set(_parseState 9)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 11)
      else()
        list(APPEND _defines "${_compileArg}")
      endif()
    elseif(_parseState EQUAL 9)
      set(_chainedPchFilePath    "${_compileArg}")
      set(_chainedPchFilePathSet YES)
      set(_parseState 10)
    elseif((_parseState EQUAL 10) AND (_compileArg MATCHES "^OPTIONS$"))
      set(_parseState 11)
    elseif(_parseState EQUAL 11)
      list(APPEND _options "${_compileArg}")
    else()
      message(FATAL_ERROR "Invalid parameter token near \"${_compileArg}\".")
    endif()
  endforeach()
  if(NOT ((_parseState EQUAL 6) OR (_parseState EQUAL 8) OR (_parseState EQUAL 10) OR (_parseState EQUAL 11)))
    message(FATAL_ERROR "Invalid number of parameters.")
  endif()


  get_filename_component(_outPchFileName "${_outPchFilePath}" NAME)
  get_filename_component(_outPchFileDir  "${_outPchFilePath}" PATH)
  set(_pchTempFilePath "${_outPchFilePath}_pch.tmp")

  set(_defineFlags)
  foreach(_define ${_defines})
    string(REPLACE ";" "\;" _define "${_define}")
    list(APPEND _defineFlags "-D${_define}")
  endforeach()

  if(_chainedPchFilePathSet)
    set(_chainedPchFlags "-include-pch" "${_chainedPchFilePath}")
  else()
    set(_chainedPchFlags)
  endif()

  # Header precompilation is triggered by CLANG change, change of source headers or change in chained precompiled header.
  add_custom_command(
      OUTPUT "${_pchTempFilePath}"
      COMMAND "${CMAKE_COMMAND}" -E make_directory "${_outPchFileDir}"
rishipal's avatar
rishipal committed
322
      COMMAND IGC_TARGET__TOOLS_CLANG -cc1 -x cl -fblocks -fpreserve-vec3-type -Os -opencl-builtins "-triple=${_archTriple}" -cl-std=CL2.0 -emit-pch -o "${_pchTempFilePath}" ${_chainedPchFlags} ${_defineFlags} ${_options} ${_srcHeaderFilePaths}
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
      DEPENDS GetClang ${_chainedPchFilePath} ${_srcHeaderFilePaths}
      COMMENT "BiF: Building OpenCL precompiled header: \"${_outPchFileName}\""
    )
  add_custom_command(
      OUTPUT "${_outPchFilePath}"
      COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_pchTempFilePath}" "${_outPchFilePath}"
      DEPENDS "${_pchTempFilePath}"
      COMMENT "BiF: Updating precompiled header \"${_outPchFileName}\" if changed."
    )
endfunction()

# ======================================================================================================

# Creates custom command which builds LLVM Bitcode file for built-in functions.
#
# igc_bif_build_bc(
#     OUTPUT               outBcFilePath
#     TRIPLE               archTriple
#     SOURCES              srcFilePath [srcFilePath [...]]
#     [LANGUAGE            srcLanguage]
#     [PRECOMPILED         pchFilePath]
#     [FORCE_INCLUDE       incFilePath [incFilePath [...]]]
#     [INCLUDE_DIRECTORIES includeDir [includeDir [...]]]
#     [DEFINES             define [define [...]]]
#     [DEPENDS             dependency [dependency [...]]]
#     [OPTIMIZE            useOpt]
#     [OPTIONS             lang [option [...]]]
#     [OPTIONS             lang [option [...]]]
#     [...]
#     [OPTIONS             lang [option [...]]]
#   )
#
# @param outBcFilePath      Full path where output bitcode file should be written.
#                           Output file is only updated if it does not exist or it was changed.
# @param archTriple         Identifier for target architecture for compiled sources.
# @param srcFilePath        Path to source file which will be compiled into .bc (multiple can be specified).
#                           If multiple files are specified, each file is converted to .bc and output .bc
#                           is created by linking intermediate .bc files.
# @param srcLanguage        Language used to compile/build source files. The following are supported:
#                            - "DEFAULT" - determined by extension (.ll -> "LL"; .bc -> "BC"; .* (other) -> "CL").
#                            - "CL"      - Sources treated as OpenCL C sources (CLANG will be used).
#                            - "LL"      - Sources treated as LLVM IR text files (LLVM-AS will be used).
#                            - "BC"      - Sources treated as LLVM Bitcode files (LLVM-LINK will be used).
#                           If not specified, "DEFAULT" is used.
# @param pchFilePath        Includes precompiled header. Ignored for other files than "CL".
# @param incFilePath        Includes header for each compiled source. Ignored for other files than "CL".
# @param includeDir         Adds include directory. Ignored for other files than "CL".
# @param define             Additional preprocessor definition (multiple can be specified). Ignored for other files than "CL".
# @param useOpt             Use optmizer (boolean value). Only final created .bc is optimized (intermediate .bc created during
#                           build will not be put into optimizer).  Default is FALSE.
# @param dependency         Additional files / targets that source files depends on (multiple can be specified).
# @param lang               Language for which options are defined (currently "CL", "LL", "BC" and "OPT" for optimizer;
#                           "DEFAULT" will add options to all lanuages).
# @param option             Additional compilation option (multiple can be specified).
function(igc_bif_build_bc)
  set(__allowedLanguages "DEFAULT" "CL" "LL" "BC")
  set(__allowedOptLanguages ${__allowedLanguages} "OPT")

  set(_srcLanguage "DEFAULT")
  set(_pchFilePath)
  set(_pchFilePathSet NO)
  set(_incFilePaths)
  set(_includeDirs)
  set(_defines)
  set(_dependencies)
  set(_useOpt NO)
  foreach(_allowedOptLang ${__allowedOptLanguages})
    set("_options_${_allowedOptLang}")
  endforeach()

  set(_parseState 0)
  foreach(_compileArg ${ARGN})
    string(REPLACE ";" "\;" _compileArg "${_compileArg}")

    # States: [0] "OUTPUT" [1] <param> [2] "TRIPLE" [3] <param> [4] "SOURCES" [5] <param> [6] *<param> [6]
    #         *1( "LANGUAGE" [7] <param> [8] ) *1( "PRECOMPILED" [9] <param> [10] ) *1( "FORCE_INCLUDE" [11] <param> [12] *<param> [12] )
    #         *1( "INCLUDE_DIRECTORIES" [13] <param> [14] *<param> [14] ) *1( "DEFINES" [15] <param> [16] *<param> [16] ) *1( "DEPENDS" [17] <param> [18] *<param> [18] )
    #         *1( "OPTIMIZE" [19] <param> [20] ) *( "OPTIONS" [21] <param> [22] *<param> [22] )
    # Transitions: 0 (OUTPUT) -> 1 -> 2 (TRIPLE) -> 3 -> 4 (SOURCES) -> 5 -> 6
    #              6 (LANGUAGE) -> 7 -> 8
    #              6 (PRECOMPILED) -> 9 -> 10
    #              6 (FORCE_INCLUDE) -> 11 -> 12
    #              6 (INCLUDE_DIRECTORIES) -> 13 -> 14
    #              6 (DEFINES) -> 15 -> 16
    #              6 (DEPENDS) -> 17 -> 18
    #              6 (OPTIMIZE) -> 19 -> 20
    #              6 (OPTIONS) -> 21 -> 22
    #              6 -> 6
    #              8 (PRECOMPILED) -> 9 -> 10
    #              8 (FORCE_INCLUDE) -> 11 -> 12
    #              8 (INCLUDE_DIRECTORIES) -> 13 -> 14
    #              8 (DEFINES) -> 15 -> 16
    #              8 (DEPENDS) -> 17 -> 18
    #              8 (OPTIMIZE) -> 19 -> 20
    #              8 (OPTIONS) -> 21 -> 22
    #              10 (FORCE_INCLUDE) -> 11 -> 12
    #              10 (INCLUDE_DIRECTORIES) -> 13 -> 14
    #              10 (DEFINES) -> 15 -> 16
    #              10 (DEPENDS) -> 17 -> 18
    #              10 (OPTIMIZE) -> 19 -> 20
    #              10 (OPTIONS) -> 21 -> 22
    #              12 (INCLUDE_DIRECTORIES) -> 13 -> 14
    #              12 (DEFINES) -> 15 -> 16
    #              12 (DEPENDS) -> 17 -> 18
    #              12 (OPTIMIZE) -> 19 -> 20
    #              12 (OPTIONS) -> 21 -> 22
    #              12 -> 12
    #              14 (DEFINES) -> 15 -> 16
    #              14 (DEPENDS) -> 17 -> 18
    #              14 (OPTIMIZE) -> 19 -> 20
    #              14 (OPTIONS) -> 21 -> 22
    #              14 -> 14
    #              16 (DEPENDS) -> 17 -> 18
    #              16 (OPTIMIZE) -> 19 -> 20
    #              16 (OPTIONS) -> 21 -> 22
    #              16 -> 16
    #              18 (OPTIMIZE) -> 19 -> 20
    #              18 (OPTIONS) -> 21 -> 22
    #              18 -> 18
    #              20 (OPTIONS) -> 21 -> 22
    #              22 (OPTIONS) -> 21 -> 22
    #              22 -> 22
    # Stop States: 6, 8, 10, 12, 14, 16, 18, 20, 22
    if((_parseState EQUAL 0) AND (_compileArg MATCHES "^OUTPUT$"))
      set(_parseState 1)
    elseif(_parseState EQUAL 1)
      set(_outBcFilePath "${_compileArg}")
      set(_parseState 2)
    elseif((_parseState EQUAL 2) AND (_compileArg MATCHES "^TRIPLE$"))
      set(_parseState 3)
    elseif(_parseState EQUAL 3)
      set(_archTriple "${_compileArg}")
      set(_parseState 4)
    elseif((_parseState EQUAL 4) AND (_compileArg MATCHES "^SOURCES$"))
      set(_parseState 5)
    elseif(_parseState EQUAL 5)
      set(_srcFilePaths "${_compileArg}")
      set(_parseState 6)
    elseif(_parseState EQUAL 6)
      if(_compileArg MATCHES "^LANGUAGE$")
        set(_parseState 7)
      elseif(_compileArg MATCHES "^PRECOMPILED$")
        set(_parseState 9)
      elseif(_compileArg MATCHES "^FORCE_INCLUDE$")
        set(_parseState 11)
      elseif(_compileArg MATCHES "^INCLUDE_DIRECTORIES$")
        set(_parseState 13)
      elseif(_compileArg MATCHES "^DEFINES$")
        set(_parseState 15)
      elseif(_compileArg MATCHES "^DEPENDS$")
        set(_parseState 17)
      elseif(_compileArg MATCHES "^OPTIMIZE$")
        set(_parseState 19)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      else()
        list(APPEND _srcFilePaths "${_compileArg}")
      endif()
    elseif(_parseState EQUAL 7)
      list(FIND __allowedLanguages "${_compileArg}" _foundLanguageIdx)
      if(_foundLanguageIdx LESS 0)
        message(FATAL_ERROR "Language \"${_compileArg}\" is not supported. Supported languages: ${__allowedLanguages}")
      endif()
      set(_srcLanguage "${_compileArg}")
      set(_parseState 8)
    elseif(_parseState EQUAL 8)
      if(_compileArg MATCHES "^PRECOMPILED$")
        set(_parseState 9)
      elseif(_compileArg MATCHES "^FORCE_INCLUDE$")
        set(_parseState 11)
      elseif(_compileArg MATCHES "^INCLUDE_DIRECTORIES$")
        set(_parseState 13)
      elseif(_compileArg MATCHES "^DEFINES$")
        set(_parseState 15)
      elseif(_compileArg MATCHES "^DEPENDS$")
        set(_parseState 17)
      elseif(_compileArg MATCHES "^OPTIMIZE$")
        set(_parseState 19)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      else()
        message(FATAL_ERROR "Invalid parameter token near \"${_compileArg}\".")
      endif()
    elseif(_parseState EQUAL 9)
      set(_pchFilePath "${_compileArg}")
      set(_pchFilePathSet YES)
      set(_parseState 10)
    elseif(_parseState EQUAL 10)
      if(_compileArg MATCHES "^FORCE_INCLUDE$")
        set(_parseState 11)
      elseif(_compileArg MATCHES "^INCLUDE_DIRECTORIES$")
        set(_parseState 13)
      elseif(_compileArg MATCHES "^DEFINES$")
        set(_parseState 15)
      elseif(_compileArg MATCHES "^DEPENDS$")
        set(_parseState 17)
      elseif(_compileArg MATCHES "^OPTIMIZE$")
        set(_parseState 19)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      else()
        message(FATAL_ERROR "Invalid parameter token near \"${_compileArg}\".")
      endif()
    elseif(_parseState EQUAL 11)
      set(_incFilePaths "${_compileArg}")
      set(_parseState 12)
    elseif(_parseState EQUAL 12)
      if(_compileArg MATCHES "^INCLUDE_DIRECTORIES$")
        set(_parseState 13)
      elseif(_compileArg MATCHES "^DEFINES$")
        set(_parseState 15)
      elseif(_compileArg MATCHES "^DEPENDS$")
        set(_parseState 17)
      elseif(_compileArg MATCHES "^OPTIMIZE$")
        set(_parseState 19)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      else()
        list(APPEND _incFilePaths "${_compileArg}")
      endif()
    elseif(_parseState EQUAL 13)
      set(_includeDirs "${_compileArg}")
      set(_parseState 14)
    elseif(_parseState EQUAL 14)
      if(_compileArg MATCHES "^DEFINES$")
        set(_parseState 15)
      elseif(_compileArg MATCHES "^DEPENDS$")
        set(_parseState 17)
      elseif(_compileArg MATCHES "^OPTIMIZE$")
        set(_parseState 19)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      else()
        list(APPEND _includeDirs "${_compileArg}")
      endif()
    elseif(_parseState EQUAL 15)
      set(_defines "${_compileArg}")
      set(_parseState 16)
    elseif(_parseState EQUAL 16)
      if(_compileArg MATCHES "^DEPENDS$")
        set(_parseState 17)
      elseif(_compileArg MATCHES "^OPTIMIZE$")
        set(_parseState 19)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      else()
        list(APPEND _defines "${_compileArg}")
      endif()
    elseif(_parseState EQUAL 17)
      set(_dependencies "${_compileArg}")
      set(_parseState 18)
    elseif(_parseState EQUAL 18)
      if(_compileArg MATCHES "^OPTIMIZE$")
        set(_parseState 19)
      elseif(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      else()
        list(APPEND _dependencies "${_compileArg}")
      endif()
    elseif(_parseState EQUAL 19)
      set(_useOpt "${_compileArg}")
      set(_parseState 20)
    elseif((_parseState EQUAL 20) AND (_compileArg MATCHES "^OPTIONS$"))
      set(_parseState 21)
    elseif(_parseState EQUAL 21)
      list(FIND __allowedOptLanguages "${_compileArg}" _foundLanguageIdx)
      if(_foundLanguageIdx LESS 0)
        message(WARNING "Options language \"${_compileArg}\" is not supported. These options will be omitted. Supported languages: ${__allowedOptLanguages}")
        set(_optionsLang)
      else()
        set(_optionsLang "${_compileArg}")
      endif()
      set(_parseState 22)
    elseif(_parseState EQUAL 22)
      if(_compileArg MATCHES "^OPTIONS$")
        set(_parseState 21)
      elseif(DEFINED _optionsLang)
        list(APPEND "_options_${_optionsLang}" "${_compileArg}")
      endif()
    else()
      message(FATAL_ERROR "Invalid parameter token near \"${_compileArg}\".")
    endif()
  endforeach()
  if(NOT ((_parseState EQUAL 6) OR (_parseState EQUAL 8) OR (_parseState EQUAL 10) OR (_parseState EQUAL 12) OR (_parseState EQUAL 14) OR (_parseState EQUAL 16) OR (_parseState EQUAL 18) OR (_parseState EQUAL 20) OR (_parseState EQUAL 22)))
    message(FATAL_ERROR "Invalid number of parameters.")
  endif()


  get_filename_component(_outBcFileName "${_outBcFilePath}" NAME)
  get_filename_component(_outBcFileDir  "${_outBcFilePath}" PATH)

  # Grouping source files by language.
  foreach(_allowedLang ${__allowedLanguages})
    set("_srcFilePaths_${_allowedLang}")
  endforeach()
  if(_srcLanguage MATCHES "^DEFAULT$")
    foreach(_srcFilePath ${_srcFilePaths})
      string(REPLACE ";" "\;" _srcFilePath "${_srcFilePath}")
      if(_srcFilePath MATCHES "\\.[lL][lL]$")
        set(_srcFileLang "LL")
      elseif(_srcFilePath MATCHES "\\.[bB][cC]$")
        set(_srcFileLang "BC")
      else()
        set(_srcFileLang "CL")
      endif()
      list(APPEND "_srcFilePaths_${_srcFileLang}" "${_srcFilePath}")
    endforeach()
  else()
    set("_srcFilePaths_${_srcLanguage}" ${_srcFilePaths})
  endif()

  if(_pchFilePathSet)
    set(_pchFlags "-include-pch" "${_pchFilePath}")
  else()
    set(_pchFlags)
  endif()

  set(_incFileFlags)
  foreach(_incFilePath ${_incFilePaths})
    string(REPLACE ";" "\;" _incFilePath "${_incFilePath}")
    file(TO_CMAKE_PATH "${_incFilePath}" _incFilePath)
    list(APPEND _incFileFlags "-include" "${_incFilePath}")
  endforeach()

  set(_includeDirsFlags)
  foreach(_includeDir ${_includeDirs})
    string(REPLACE ";" "\;" _includeDir "${_includeDir}")
    list(APPEND _includeDirsFlags "-I" "${_includeDir}")
  endforeach()

  set(_defineFlags)
  foreach(_define ${_defines})
    string(REPLACE ";" "\;" _define "${_define}")
    list(APPEND _defineFlags "-D${_define}")
  endforeach()


  # Adding custom build commands for each language.
  set(_bcFiles)
  set(_bcFileId 0)
  foreach(_srcFilePath ${_srcFilePaths_CL})
    string(REPLACE ";" "\;" _srcFilePath "${_srcFilePath}")

    get_filename_component(_srcFileName      "${_srcFilePath}" NAME)
    get_filename_component(_srcFileNameWoExt "${_srcFilePath}" NAME_WE)
    set(_bcIntFilePath  "${_outBcFilePath}_${_srcFileNameWoExt}__cl__${_bcFileId}.bc")
    set(_bcTempFilePath "${_bcIntFilePath}.tmp")
    
    file(TO_CMAKE_PATH "${_bcIntFilePath}" _bcIntFilePath)
    file(TO_CMAKE_PATH "${_bcTempFilePath}" _bcTempFilePath)
    
    math(EXPR _bcFileId "${_bcFileId} + 1")

    # OpenCL source compilation is triggered by CLANG change, change of source files, change of precompiled header, change of
    # forcibly included headers or change of additional dependencies.
    add_custom_command(
        OUTPUT "${_bcTempFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E make_directory "${_outBcFileDir}"
rishipal's avatar
rishipal committed
681
        COMMAND IGC_TARGET__TOOLS_CLANG -cc1 -x cl -fblocks -fpreserve-vec3-type -opencl-builtins "-triple=${_archTriple}" -w -emit-llvm-bc -o "${_bcTempFilePath}" ${_pchFlags} ${_incFileFlags} ${_includeDirsFlags} ${_defineFlags} ${_options_DEFAULT} ${_options_CL} "${_srcFilePath}"
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
        DEPENDS GetClang ${_pchFilePath} ${_incFilePaths} "${_srcFilePath}" ${_dependencies}
        COMMENT "BiF: \"${_outBcFileName}\": Compiling OpenCL source: \"${_srcFileName}\""
      )
    add_custom_command(
        OUTPUT "${_bcIntFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_bcTempFilePath}" "${_bcIntFilePath}"
        DEPENDS "${_bcTempFilePath}"
        COMMENT "BiF: \"${_outBcFileName}\": Updating intermediate .bc for \"${_srcFileName}\" if changed."
      )

    list(APPEND _bcFiles "${_bcIntFilePath}")
  endforeach()

  foreach(_srcFilePath ${_srcFilePaths_LL})
    string(REPLACE ";" "\;" _srcFilePath "${_srcFilePath}")

    get_filename_component(_srcFileName      "${_srcFilePath}" NAME)
    get_filename_component(_srcFileNameWoExt "${_srcFilePath}" NAME_WE)
    set(_bcIntFilePath  "${_outBcFilePath}_${_srcFileNameWoExt}__ll__${_bcFileId}.bc")
    set(_bcTempFilePath "${_bcIntFilePath}.tmp")
    math(EXPR _bcFileId "${_bcFileId} + 1")

    # LLVM assembling is triggered by host LLVM package change (where tools are), change of source files or change of additional dependencies.
    # Makes sure that LLVM is unzipped before.
    add_custom_command(
        OUTPUT "${_bcTempFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E make_directory "${_outBcFileDir}"
709
        COMMAND llvm-as -o "${_bcTempFilePath}" ${_options_DEFAULT} ${_options_LL} "${_srcFilePath}"
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
        DEPENDS "${_srcFilePath}" ${_dependencies}
        COMMENT "BiF: \"${_outBcFileName}\": Compiling OpenCL source: \"${_srcFileName}\""
      )
    add_custom_command(
        OUTPUT "${_bcIntFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_bcTempFilePath}" "${_bcIntFilePath}"
        DEPENDS "${_bcTempFilePath}"
        COMMENT "BiF: \"${_outBcFileName}\": Updating intermediate .bc for \"${_srcFileName}\" if changed."
      )

    list(APPEND _bcFiles "${_bcIntFilePath}")
  endforeach()

  foreach(_srcFilePath ${_srcFilePaths_BC})
    string(REPLACE ";" "\;" _srcFilePath "${_srcFilePath}")
    list(APPEND _bcFiles "${_srcFilePath}")
  endforeach()

  # Determining whether implicit link is required.
  list(LENGTH _bcFiles _bcFilesCount)
  if(_bcFilesCount GREATER 1)
    set(_bcIntFilePath  "${_outBcFilePath}__link__${_bcFileId}.bc")
    set(_bcTempFilePath "${_bcIntFilePath}.tmp")
    math(EXPR _bcFileId "${_bcFileId} + 1")

    # LLVM linking is triggered by host LLVM package change (where tools are), change of intermediate .bc files or change of additional dependencies.
    # Makes sure that LLVM is unzipped before.
    add_custom_command(
        OUTPUT "${_bcTempFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E make_directory "${_outBcFileDir}"
740
        COMMAND llvm-link -o "${_bcTempFilePath}" ${_options_DEFAULT} ${_options_BC} ${_bcFiles}
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
        DEPENDS ${_bcFiles} ${_dependencies}
        COMMENT "BiF: \"${_outBcFileName}\": Linking intermediate .bc into output .bc."
      )
    add_custom_command(
        OUTPUT "${_bcIntFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_bcTempFilePath}" "${_bcIntFilePath}"
        DEPENDS "${_bcTempFilePath}"
        COMMENT "BiF: \"${_outBcFileName}\": Updating output .bc if changed (after linking)."
      )

    set(_bcFiles "${_bcIntFilePath}")
  elseif(_bcFilesCount LESS 1)
    return()
  endif()

  if(_useOpt)
    set(_bcIntFilePath  "${_outBcFilePath}__opt__${_bcFileId}.bc")
    set(_bcTempFilePath "${_bcIntFilePath}.tmp")

    # LLVM optmizer is triggered by host OPT change, change of .bc file to optimize or change of additional dependencies.
    # Makes sure that LLVM is unzipped before.
    add_custom_command(
        OUTPUT "${_bcTempFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E make_directory "${_outBcFileDir}"
765 766
        COMMAND opt -O2 -o "${_bcTempFilePath}" ${_options_DEFAULT} ${_options_OPT} ${_bcFiles}
        DEPENDS ${_bcFiles} ${_dependencies}
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
        COMMENT "BiF: \"${_outBcFileName}\": Optmizing output .bc."
      )
    add_custom_command(
        OUTPUT "${_bcIntFilePath}"
        COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${_bcTempFilePath}" "${_bcIntFilePath}"
        DEPENDS "${_bcTempFilePath}"
        COMMENT "BiF: \"${_outBcFileName}\": Updating output .bc if changed (after optimization)."
      )

    set(_bcFiles "${_bcIntFilePath}")
  endif()

  # Final copy.
  #igc_tool_get_activity_commandline(_copyOutCmd IGC_TARGET__TOOLS_COPY CopyPreserve ${_bcFiles} "${_outBcFilePath}")
  add_custom_command(
      OUTPUT "${_outBcFilePath}"
      COMMAND "${CMAKE_COMMAND}" -E copy_if_different ${_bcFiles} "${_outBcFilePath}"
      DEPENDS ${_bcFiles}
      COMMENT "BiF: \"${_outBcFileName}\": Copying output .bc."
    )
endfunction()

# ======================================================================================================

# Returns list common OpenCL C files from selected directories:
# - sources (.cl)
# - headers (.h)
#
# @param retVarName         Name of placeholder variable where results will be stored.
# @param [includeDir [...]] Full path to directory which will be searched for common files.
function(igc_bif_find_cl_files retVarName)
  set(_filePaths)
  foreach(_includeDir ${ARGN})
    string(REPLACE ";" "\;" _includeDir "${_includeDir}")
    file(GLOB_RECURSE _sourceFilePaths "${_includeDir}/*.[cC][lL]")
    file(GLOB_RECURSE _headerFilePaths "${_includeDir}/*.[hH]")
    list(APPEND _filePaths ${_sourceFilePaths} ${_headerFilePaths})
  endforeach()
  set("${retVarName}" ${_filePaths} PARENT_SCOPE)
endfunction()

# ======================================================================================================
# ======================================================================================================
# ======================================================================================================

# ======================================================================================================
# ===================================== BUILD STEP CONFIGURATION =======================================
# ======================================================================================================

# ======================================== Precompiled headers =========================================

set(IGC_BUILD__BIF_OCL_PCH_SRC "${IGC_TARGET__TOOLS_CLANG_DIR}/opencl-c.h")
819 820 821 822
add_custom_command(
    OUTPUT ${IGC_BUILD__BIF_OCL_PCH_SRC}
    DEPENDS GetClang
)
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869

set(IGC_BUILD__BIF_RS_PCH_SRC "${IGC_OPTION__BIF_SRC_RS_DIR}/rs_pch.h")
set(IGC_BUILD__BIF_RS_PCH32   "${IGC_BUILD__BIF_DIR}/rs32.pch")
set(IGC_BUILD__BIF_RS_PCH64   "${IGC_BUILD__BIF_DIR}/rs64.pch")

igc_bif_build_pch(
    OUTPUT  "${IGC_BUILD__BIF_RS_PCH32}"
    TRIPLE  spir
    HEADERS "${IGC_BUILD__BIF_RS_PCH_SRC}"
    DEFINES "__32bit__=1" "cl_khr_fp64"
    OPTIONS -include ${IGC_BUILD__BIF_OCL_PCH_SRC}
  )
  
igc_bif_build_pch(
    OUTPUT  "${IGC_BUILD__BIF_RS_PCH64}"
    TRIPLE  spir64
    HEADERS "${IGC_BUILD__BIF_RS_PCH_SRC}"
    OPTIONS -include ${IGC_BUILD__BIF_OCL_PCH_SRC}
  )

# ======================================================================================================
#concat files function
# @param in_file        input file.
# @param out_file       output file.
function(cat in_file out_file)
  file(READ ${in_file} CONTENTS)
  file(APPEND ${out_file} "${CONTENTS}")
endfunction()

# ============================================ Building .bc =============================================

# All:
#     -cc1 -x cl -opencl-builtins -w -emit-llvm-bc
#     -include .\IGCBiFModule\opencl_cth.h
#     -I .\IGCBiFModule\shared -I .\IGCBiFModule\PointerSize
#     -triple=(spir or spir64 - consistent with PCH)
### -include-pch OCL_32                 -D__EXECUTION_MODEL_DEBUG=1 -D__OPENCL_C_VERSION__=200                                      .\IGCBiFModule\shared\IBiF_Impl.cl                   -o .\IBiF_Impl_int.bc
### -include-pch OCL_32 -cl-enable-half -D__EXECUTION_MODEL_DEBUG=1 -D__OPENCL_C_VERSION__=120 -Dcl_khr_fp16 -I                     .\IGCBiFModule\PointerSize\IBiF_size_t.cl            -o .\IGCsize_t_32.bc
### -include-pch OCL_64 -cl-enable-half -D__EXECUTION_MODEL_DEBUG=1 -D__OPENCL_C_VERSION__=120 -Dcl_khr_fp16 -I                     .\IGCBiFModule\PointerSize\IBiF_size_t.cl            -o .\IGCsize_t_64.bc

# Other:
#     %LLVM_LINK_EXE% .\IBiF_Impl_int.bc .\IGCBiF_Impl_ll.bc -o .\OCLBiFImpl.bc


file(GLOB_RECURSE _PRE_RELEASE_CL 	"${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/PreRelease/*.cl")

set(_concatScript   "${IGC_SOURCE_DIR}/BiFModule/concat.py")
870
set(IGC_BUILD__BIF_OCL_FORCE_INC  "${IGC_BUILD__BIF_DIR}/opencl_cth.h")
871 872 873
set(IGC_BUILD__BIF_OCL_SHARED_INC  "${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/opencl_cth_released.h")
set(IGC_BUILD__BIF_OCL_SHARED_INC_PRE_RELEASE    "${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/PreRelease/opencl_cth_pre_release.h")

Junjie Gu's avatar
Junjie Gu committed
874 875


876 877
add_custom_command(
    OUTPUT "${IGC_BUILD__BIF_OCL_FORCE_INC}"
878
    COMMAND "${CMAKE_COMMAND}" -E make_directory "${IGC_BUILD__BIF_DIR}"
879
    COMMAND "${PYTHON_EXECUTABLE}"
Junjie Gu's avatar
Junjie Gu committed
880
    ARGS ${_concatScript} -new ${IGC_BUILD__BIF_OCL_FORCE_INC} ${IGC_BUILD__BIF_OCL_SHARED_INC} ${IGC_BUILD__BIF_OCL_SHARED_INC_PRE_RELEASE}
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
    DEPENDS ${_concatScript}
    DEPENDS ${IGC_BUILD__BIF_OCL_SHARED_INC}
    DEPENDS ${IGC_BUILD__BIF_OCL_SHARED_INC_PRE_RELEASE}
    COMMENT "Running concat.py for autogenerating opencl_cth.h"
    )

set(IGC_BUILD__BIF_OCL_COMMON_INC_DIRS
    "${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL"
    "${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/PointerSize"
    "${IGC_OPTION__BIF_SRC_OCL_DIR}/Headers"
    "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/ocl_igc_shared/device_enqueue"
  )

set(IGC_BUILD__BIF_SPIRV_COMMON_INC_DIRS
    "${IGC_OPTION__BIF_SRC_OCL_DIR}/Implementation"
    "${IGC_OPTION__BIF_SRC_OCL_DIR}/Headers"
    "${CMAKE_CURRENT_SOURCE_DIR}/../AdaptorOCL/ocl_igc_interface/device_enqueue"
  )
  
file(GLOB_RECURSE _MATH_SRC_BC      "${IGC_OPTION__BIF_SRC_OCL_DIR}/*.bc")

file(GLOB_RECURSE SVML_FILES "${IGC_OPTION__BIF_SRC_OCL_DIR}/Implementation/BinaryReleaseOnly/*.cl" )

set(FLAG "")
905 906 907 908
if(${VME_TYPES_DEFINED})
    list(APPEND FLAG "__VME_TYPES_DEFINED__")
endif(VME_TYPES_DEFINED)

909 910 911

igc_bif_find_cl_files(IGC_BUILD__BIF_OCL_COMMON_DEPENDS ${IGC_BUILD__BIF_OCL_COMMON_INC_DIRS} "${IGC_OPTION__BIF_SRC_OCL_DIR}/Implementation")

912
set(KHR_DEFINES "cl_khr_f16" "cl_khrfp64" "cl_khr_gl_msaa_sharing" "cl_khr_mipmap_image" "cl_khr_depth_images" "cl_intel_subgroups_short" "cl_intel_subgroups_char" "cl_intel_subgroups_long" "cl_intel_64bit_global_atomics_placeholder")
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IBiF_Impl_int.bc"
    TRIPLE               spir64
    SOURCES              "${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/IBiF_Impl.cl"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
    INCLUDE_DIRECTORIES  ${IGC_BUILD__BIF_OCL_COMMON_INC_DIRS}
    DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=200" "__IGC_BUILD__" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
    OPTIONS CL           -cl-std=CL2.0
  )

igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IBiF_Impl_int_spirv.bc"
    TRIPLE               spir64
    SOURCES              "${IGC_OPTION__BIF_SRC_OCL_DIR}/Implementation/IBiF_Impl.cl"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC}
    INCLUDE_DIRECTORIES  ${IGC_BUILD__BIF_SPIRV_COMMON_INC_DIRS}
    DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=200" "__IGC_BUILD__" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
    OPTIONS CL           -cl-std=CL2.0
  )
  if ( NOT "${_PRE_RELEASE_CL}" STREQUAL "" )
   igc_bif_build_bc(
      OUTPUT               "${IGC_BUILD__BIF_DIR}/IBiF_PreRelease_int.bc"
      TRIPLE               spir64
      SOURCES              ${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/PreRelease/IBIF_PreRelease_Impl.cl
      FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
      INCLUDE_DIRECTORIES  ${IGC_BUILD__BIF_OCL_COMMON_INC_DIRS}
      DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=200" "__IGC_BUILD__" ${KHR_DEFINES} "cl_intel_device_side_avc_vme_enable" ${FLAG}
      DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
      OPTIONS CL           -cl-std=CL2.0
     )
 endif()
igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IBiF_spirv_size_t_32.bc"
    TRIPLE               spir
    SOURCES              "${IGC_OPTION__BIF_SRC_OCL_DIR}/Implementation/pointersize.cl"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC}  ${IGC_BUILD__BIF_OCL_FORCE_INC}
    INCLUDE_DIRECTORIES  ${IGC_BUILD__BIF_OCL_COMMON_INC_DIRS}
    DEFINES              "__32bit__=1" "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=200" "__IGC_BUILD__" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
    OPTIONS CL           -cl-std=CL2.0
  )
igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IBiF_spirv_size_t_64.bc"
    TRIPLE               spir64
    SOURCES              "${IGC_OPTION__BIF_SRC_OCL_DIR}/Implementation/pointersize.cl"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
    INCLUDE_DIRECTORIES  ${IGC_BUILD__BIF_OCL_COMMON_INC_DIRS}
    DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=200" "__IGC_BUILD__" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
    OPTIONS CL           -cl-std=CL2.0
  )
igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IGCsize_t_32_int.bc"
    TRIPLE               spir
    SOURCES              "${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/PointerSize/IBiF_size_t.cl"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC}  ${IGC_BUILD__BIF_OCL_FORCE_INC}
    INCLUDE_DIRECTORIES  ${IGC_BUILD__BIF_OCL_COMMON_INC_DIRS}
    DEFINES              "__32bit__=1" "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=200" "__IGC_BUILD__" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
    OPTIONS CL           -cl-std=CL2.0
  )
igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IGCsize_t_64_int.bc"
    TRIPLE               spir64
    SOURCES              "${IGC_OPTION__BIF_SRC_OCL_DIR}/Languages/OpenCL/PointerSize/IBiF_size_t.cl"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
    INCLUDE_DIRECTORIES  ${IGC_BUILD__BIF_OCL_COMMON_INC_DIRS}
    DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=200" "__IGC_BUILD__" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
    OPTIONS CL           -cl-std=CL2.0
  )
igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IGCsize_t_32.bc"
    TRIPLE               spir
    SOURCES              "${IGC_BUILD__BIF_DIR}/IGCsize_t_32_int.bc"
                         "${IGC_BUILD__BIF_DIR}/IBiF_spirv_size_t_32.bc"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
    DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=120" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
  )
igc_bif_build_bc(
    OUTPUT               "${IGC_BUILD__BIF_DIR}/IGCsize_t_64.bc"
    TRIPLE               spir64
    SOURCES              "${IGC_BUILD__BIF_DIR}/IGCsize_t_64_int.bc"
                         "${IGC_BUILD__BIF_DIR}/IBiF_spirv_size_t_64.bc"
    FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
    DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=120" ${KHR_DEFINES} ${FLAG}
    DEPENDS              ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
  )

if ("${FLAG}" STREQUAL "")
    unset(FLAG)
endif()

# TODO:
# Math source( svml files) is not released as a part of open source bundle. Instead, we release .bc.
# We basically look for .bc and only if .bc is not present we build from source.
#Currently, not sure where exactly the .bc will be placed. But ones we have all
# the info, we need to make sure all the dependencies are handled. Like for example if any
# file uses svml files, we need to prototype the headers and during linking it gets resolved.
  
  if ("${_PRE_RELEASE_CL}" STREQUAL "" AND ( NOT "${_MATH_SRC_BC}" STREQUAL "" ) )
	igc_bif_build_bc(
		OUTPUT               "${IGC_BUILD__BIF_DIR}/OCLBiFImpl.bc"
		TRIPLE               spir64
		SOURCES              "${IGC_BUILD__BIF_DIR}/IBiF_Impl_int.bc"
                             "${IGC_BUILD__BIF_DIR}/IBiF_Impl_int_spirv.bc"
							 "${IGC_BUILD__BIF_DIR}/IBiF_Math_Src_spirv.bc"
		FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
		DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=120" ${KHR_DEFINES}
	)
else()
	igc_bif_build_bc(
		OUTPUT               "${IGC_BUILD__BIF_DIR}/OCLBiFImpl.bc"
		TRIPLE               spir64
		SOURCES              "${IGC_BUILD__BIF_DIR}/IBiF_Impl_int.bc"
                             "${IGC_BUILD__BIF_DIR}/IBiF_Impl_int_spirv.bc"
						     "${IGC_BUILD__BIF_DIR}/IBiF_PreRelease_int.bc"
		FORCE_INCLUDE        ${IGC_BUILD__BIF_OCL_PCH_SRC} ${IGC_BUILD__BIF_OCL_FORCE_INC}
		DEFINES              "__EXECUTION_MODEL_DEBUG=1" "__OPENCL_C_VERSION__=120" ${KHR_DEFINES}
	)
endif()

# =========================================== Custom targets ============================================

set(IGC_BUILD__PROJ__BiFModule_OCL       "${IGC_BUILD__PROJ_NAME_PREFIX}BiFModuleOcl")
set(IGC_BUILD__PROJ__BiFModule_OCL       "${IGC_BUILD__PROJ__BiFModule_OCL}" PARENT_SCOPE)
set(IGC_BUILD__PROJ_LABEL__BiFModule_OCL "BiFModule-OCL")

add_custom_target("${IGC_BUILD__PROJ__BiFModule_OCL}"
    DEPENDS GetClang "${IGC_BUILD__BIF_DIR}/OCLBiFImpl.bc" "${IGC_BUILD__BIF_DIR}/IGCsize_t_32.bc" "${IGC_BUILD__BIF_DIR}/IGCsize_t_64.bc" "${IGC_BUILD__BIF_DIR}/IBiF_Impl_int_spirv.bc"
    SOURCES ${IGC_BUILD__BIF_OCL_COMMON_DEPENDS}
  )
set_property(TARGET "${IGC_BUILD__PROJ__BiFModule_OCL}" PROPERTY PROJECT_LABEL "${IGC_BUILD__PROJ_LABEL__BiFModule_OCL}")

# ======================================================================================================
# ======================================================================================================
1053
# ======================================================================================================