Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
target_registry.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2019, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file target_registry.h
35  @brief Registry to handle bitcode libraries.
36 */
37 
38 #pragma once
39 
40 #include "bitcode_lib.h"
41 
42 #include <bitset>
43 #include <map>
44 #include <vector>
45 
46 // Some background information useful for understanding how this works.
47 // - static variables with global or class scope and without constructor are
48 // initialized at load time.
49 // - static variables with global or class scope, which require code execution
50 // (i.e. constructor), are initialized after load time, but before the main()
51 // is started. The order of initialization is defined only inside single
52 // translation unit. Between translation units the order is not defined by
53 // the language (defined by linker implementation). Initialization guaranteed
54 // to happen in a single thread.
55 // - function scope static variables are initialized on the first use.
56 
57 // Initialization of TargetLibRegistry happens in two stages.
58 // On the first stage libraries are registered by executing BitcodeLib constructors
59 // of their static instances. The pointers to the libs are stored in the vector
60 // (TargetLibRegistry::libs).
61 // On the second stage, an instance of TargetLibRegistry is constructed after the
62 // main() starts. During TargetLibRegistry construction TargetLibRegistry::libs
63 // vector is used to populate the class data and store information that is
64 // convenient for further access. TargetLibRegistry::libs is nullified at this point.
65 
67  static std::vector<const BitcodeLib *> *libs;
69 
70  // Dispatch
71  // Currently only one dispatch module is supported: for x86 CPUs.
72  // It's OS / arch agnostic.
73  // TODO: do we need separate dispatch module for Windows and for Unix (or any specific platform)?
75 
76  // Builtins-c
77  // OS x Arch
78  std::map<uint32_t, const BitcodeLib *> m_builtins;
79 
80  // ISPC targets
81  // Target x OS (Win/Unix) x Arch [32/64/none]
82  std::map<uint32_t, const BitcodeLib *> m_targets;
83 
84  // Bitset with supported OSes
85  std::bitset<(int)TargetOS::error> m_supported_oses;
86 
87  public:
88  static void RegisterTarget(const BitcodeLib *lib);
90 
91  // Return dispatch module if available, otherwise nullptr.
92  const BitcodeLib *getDispatchLib() const;
93 
94  // Return builtins-c module if available, otherwise nullptr.
95  const BitcodeLib *getBuiltinsCLib(TargetOS os, Arch arch) const;
96 
97  // Return target module if available, otherwise nullptr.
98  const BitcodeLib *getISPCTargetLib(ISPCTarget target, TargetOS os, Arch arch) const;
99 
100  // Print user-friendly message about supported targets
101  void printSupportMatrix() const;
102 
103  std::string getSupportedArchs();
104  std::string getSupportedTargets();
105  std::string getSupportedOSes();
106 
107  bool isSupported(ISPCTarget target, TargetOS os, Arch arch) const;
108 };
ISPCTarget
Definition: target_enums.h:55
TargetOS
Definition: target_enums.h:43
std::map< uint32_t, const BitcodeLib * > m_targets
const BitcodeLib * getISPCTargetLib(ISPCTarget target, TargetOS os, Arch arch) const
bool isSupported(ISPCTarget target, TargetOS os, Arch arch) const
std::string getSupportedTargets()
std::map< uint32_t, const BitcodeLib * > m_builtins
std::string getSupportedOSes()
static std::vector< const BitcodeLib * > * libs
static TargetLibRegistry * getTargetLibRegistry()
const BitcodeLib * m_dispatch
void printSupportMatrix() const
const BitcodeLib * getBuiltinsCLib(TargetOS os, Arch arch) const
static void RegisterTarget(const BitcodeLib *lib)
std::bitset<(int) TargetOS::error > m_supported_oses
a header to host BitcodeLib - a wrapper for single bitcode library.
const BitcodeLib * getDispatchLib() const
Arch
Definition: target_enums.h:50
std::string getSupportedArchs()