placeholder.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Copyright (c) 2018 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """A number of placeholders and their rules for expansion when used in tests.
  15. These placeholders, when used in spirv_args or expected_* variables of
  16. SpirvTest, have special meanings. In spirv_args, they will be substituted by
  17. the result of instantiate_for_spirv_args(), while in expected_*, by
  18. instantiate_for_expectation(). A TestCase instance will be passed in as
  19. argument to the instantiate_*() methods.
  20. """
  21. import os
  22. import subprocess
  23. import tempfile
  24. from string import Template
  25. class PlaceHolderException(Exception):
  26. """Exception class for PlaceHolder."""
  27. pass
  28. class PlaceHolder(object):
  29. """Base class for placeholders."""
  30. def instantiate_for_spirv_args(self, testcase):
  31. """Instantiation rules for spirv_args.
  32. This method will be called when the current placeholder appears in
  33. spirv_args.
  34. Returns:
  35. A string to replace the current placeholder in spirv_args.
  36. """
  37. raise PlaceHolderException('Subclass should implement this function.')
  38. def instantiate_for_expectation(self, testcase):
  39. """Instantiation rules for expected_*.
  40. This method will be called when the current placeholder appears in
  41. expected_*.
  42. Returns:
  43. A string to replace the current placeholder in expected_*.
  44. """
  45. raise PlaceHolderException('Subclass should implement this function.')
  46. class FileShader(PlaceHolder):
  47. """Stands for a shader whose source code is in a file."""
  48. def __init__(self, source, suffix, assembly_substr=None):
  49. assert isinstance(source, str)
  50. assert isinstance(suffix, str)
  51. self.source = source
  52. self.suffix = suffix
  53. self.filename = None
  54. # If provided, this is a substring which is expected to be in
  55. # the disassembly of the module generated from this input file.
  56. self.assembly_substr = assembly_substr
  57. def instantiate_for_spirv_args(self, testcase):
  58. """Creates a temporary file and writes the source into it.
  59. Returns:
  60. The name of the temporary file.
  61. """
  62. shader, self.filename = tempfile.mkstemp(
  63. dir=testcase.directory, suffix=self.suffix)
  64. shader_object = os.fdopen(shader, 'w')
  65. shader_object.write(self.source)
  66. shader_object.close()
  67. return self.filename
  68. def instantiate_for_expectation(self, testcase):
  69. assert self.filename is not None
  70. return self.filename
  71. class ConfigFlagsFile(PlaceHolder):
  72. """Stands for a configuration file for spirv-opt generated out of a string."""
  73. def __init__(self, content, suffix):
  74. assert isinstance(content, str)
  75. assert isinstance(suffix, str)
  76. self.content = content
  77. self.suffix = suffix
  78. self.filename = None
  79. def instantiate_for_spirv_args(self, testcase):
  80. """Creates a temporary file and writes content into it.
  81. Returns:
  82. The name of the temporary file.
  83. """
  84. temp_fd, self.filename = tempfile.mkstemp(
  85. dir=testcase.directory, suffix=self.suffix)
  86. fd = os.fdopen(temp_fd, 'w')
  87. fd.write(self.content)
  88. fd.close()
  89. return '-Oconfig=%s' % self.filename
  90. def instantiate_for_expectation(self, testcase):
  91. assert self.filename is not None
  92. return self.filename
  93. class FileSPIRVShader(PlaceHolder):
  94. """Stands for a source shader file which must be converted to SPIR-V."""
  95. def __init__(self, source, suffix, assembly_substr=None):
  96. assert isinstance(source, str)
  97. assert isinstance(suffix, str)
  98. self.source = source
  99. self.suffix = suffix
  100. self.filename = None
  101. # If provided, this is a substring which is expected to be in
  102. # the disassembly of the module generated from this input file.
  103. self.assembly_substr = assembly_substr
  104. def instantiate_for_spirv_args(self, testcase):
  105. """Creates a temporary file, writes the source into it and assembles it.
  106. Returns:
  107. The name of the assembled temporary file.
  108. """
  109. shader, asm_filename = tempfile.mkstemp(
  110. dir=testcase.directory, suffix=self.suffix)
  111. shader_object = os.fdopen(shader, 'w')
  112. shader_object.write(self.source)
  113. shader_object.close()
  114. self.filename = '%s.spv' % asm_filename
  115. cmd = [
  116. testcase.test_manager.assembler_path, asm_filename, '-o', self.filename
  117. ]
  118. process = subprocess.Popen(
  119. args=cmd,
  120. stdin=subprocess.PIPE,
  121. stdout=subprocess.PIPE,
  122. stderr=subprocess.PIPE,
  123. cwd=testcase.directory)
  124. output = process.communicate()
  125. assert process.returncode == 0 and not output[0] and not output[1]
  126. return self.filename
  127. def instantiate_for_expectation(self, testcase):
  128. assert self.filename is not None
  129. return self.filename
  130. class StdinShader(PlaceHolder):
  131. """Stands for a shader whose source code is from stdin."""
  132. def __init__(self, source):
  133. assert isinstance(source, str)
  134. self.source = source
  135. self.filename = None
  136. def instantiate_for_spirv_args(self, testcase):
  137. """Writes the source code back to the TestCase instance."""
  138. testcase.stdin_shader = self.source
  139. self.filename = '-'
  140. return self.filename
  141. def instantiate_for_expectation(self, testcase):
  142. assert self.filename is not None
  143. return self.filename
  144. class TempFileName(PlaceHolder):
  145. """Stands for a temporary file's name."""
  146. def __init__(self, filename):
  147. assert isinstance(filename, str)
  148. assert filename != ''
  149. self.filename = filename
  150. def instantiate_for_spirv_args(self, testcase):
  151. return os.path.join(testcase.directory, self.filename)
  152. def instantiate_for_expectation(self, testcase):
  153. return os.path.join(testcase.directory, self.filename)
  154. class SpecializedString(PlaceHolder):
  155. """Returns a string that has been specialized based on TestCase.
  156. The string is specialized by expanding it as a string.Template
  157. with all of the specialization being done with each $param replaced
  158. by the associated member on TestCase.
  159. """
  160. def __init__(self, filename):
  161. assert isinstance(filename, str)
  162. assert filename != ''
  163. self.filename = filename
  164. def instantiate_for_spirv_args(self, testcase):
  165. return Template(self.filename).substitute(vars(testcase))
  166. def instantiate_for_expectation(self, testcase):
  167. return Template(self.filename).substitute(vars(testcase))