builtin_function_kind.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/sem_ir/builtin_function_kind.h"
  5. #include <utility>
  6. #include "toolchain/sem_ir/file.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::SemIR {
  10. // A function that validates that a builtin was declared properly.
  11. using ValidateFn = auto(const File& sem_ir, llvm::ArrayRef<TypeId> arg_types,
  12. TypeId return_type) -> bool;
  13. namespace {
  14. // Information about a builtin function.
  15. struct BuiltinInfo {
  16. llvm::StringLiteral name;
  17. ValidateFn* validate;
  18. };
  19. // The maximum number of type parameters any builtin needs.
  20. constexpr int MaxTypeParams = 2;
  21. // State used when validating a builtin signature that persists between
  22. // individual checks.
  23. struct ValidateState {
  24. // The type values of type parameters in the builtin signature. Invalid if
  25. // either no value has been deduced yet or the parameter is not used.
  26. TypeId type_params[MaxTypeParams] = {TypeId::None, TypeId::None};
  27. };
  28. template <typename TypeConstraint>
  29. auto Check(const File& sem_ir, ValidateState& state, TypeId type_id) -> bool;
  30. // Constraint that a type is generic type parameter `I` of the builtin,
  31. // satisfying `TypeConstraint`. See ValidateSignature for details.
  32. template <int I, typename TypeConstraint>
  33. struct TypeParam {
  34. static_assert(I >= 0 && I < MaxTypeParams);
  35. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  36. -> bool {
  37. if (state.type_params[I].has_value() && type_id != state.type_params[I]) {
  38. return false;
  39. }
  40. if (!TypeConstraint::Check(sem_ir, state, type_id)) {
  41. return false;
  42. }
  43. state.type_params[I] = type_id;
  44. return true;
  45. }
  46. };
  47. // Constraint that a type is a specific builtin. See ValidateSignature for
  48. // details.
  49. template <const TypeInstId& BuiltinId>
  50. struct BuiltinType {
  51. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  52. TypeId type_id) -> bool {
  53. return sem_ir.types().GetInstId(type_id) == BuiltinId;
  54. }
  55. };
  56. // Constraint that a type is a pointer to another type. See ValidateSignature
  57. // for details.
  58. template <typename PointeeT>
  59. struct PointerTo {
  60. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  61. -> bool {
  62. if (!sem_ir.types().Is<PointerType>(type_id)) {
  63. return false;
  64. }
  65. return Check<PointeeT>(sem_ir, state, sem_ir.GetPointeeType(type_id));
  66. }
  67. };
  68. // Constraint that a type is `()`, used as the return type of builtin functions
  69. // with no return value.
  70. struct NoReturn {
  71. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  72. TypeId type_id) -> bool {
  73. auto tuple = sem_ir.types().TryGetAs<TupleType>(type_id);
  74. if (!tuple) {
  75. return false;
  76. }
  77. return sem_ir.inst_blocks().Get(tuple->type_elements_id).empty();
  78. }
  79. };
  80. // Constraint that a type is `bool`.
  81. using Bool = BuiltinType<BoolType::TypeInstId>;
  82. // Constraint that a type is `Core.CharLiteral`.
  83. using CharLiteral = BuiltinType<CharLiteralType::TypeInstId>;
  84. // Constraint that a type is `u8` or an adapted type, including `Core.Char`.
  85. struct CharCompatible {
  86. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  87. TypeId type_id) -> bool {
  88. auto int_info = sem_ir.types().TryGetIntTypeInfo(type_id);
  89. if (!int_info) {
  90. // Not an integer.
  91. return false;
  92. }
  93. if (!int_info->bit_width.has_value() || int_info->is_signed) {
  94. // Must be unsigned.
  95. return false;
  96. }
  97. return sem_ir.ints().Get(int_info->bit_width) == 8;
  98. }
  99. };
  100. // Constraint that requires the type to be a sized integer type.
  101. struct AnySizedInt {
  102. static auto Check(const File& sem_ir, ValidateState& /*state*/,
  103. TypeId type_id) -> bool {
  104. return sem_ir.types().Is<IntType>(type_id);
  105. }
  106. };
  107. // Constraint that requires the type to be an integer type.
  108. struct AnyInt {
  109. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  110. -> bool {
  111. return AnySizedInt::Check(sem_ir, state, type_id) ||
  112. BuiltinType<IntLiteralType::TypeInstId>::Check(sem_ir, state,
  113. type_id);
  114. }
  115. };
  116. // Constraint that requires the type to be a float type.
  117. struct AnyFloat {
  118. static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
  119. -> bool {
  120. if (BuiltinType<LegacyFloatType::TypeInstId>::Check(sem_ir, state,
  121. type_id)) {
  122. return true;
  123. }
  124. return sem_ir.types().Is<FloatType>(type_id);
  125. }
  126. };
  127. // Constraint that requires the type to be the type type.
  128. using Type = BuiltinType<TypeType::TypeInstId>;
  129. // Checks that the specified type matches the given type constraint.
  130. template <typename TypeConstraint>
  131. auto Check(const File& sem_ir, ValidateState& state, TypeId type_id) -> bool {
  132. while (type_id.has_value()) {
  133. // Allow a type that satisfies the constraint.
  134. if (TypeConstraint::Check(sem_ir, state, type_id)) {
  135. return true;
  136. }
  137. // Also allow a class type that adapts a matching type.
  138. auto class_type = sem_ir.types().TryGetAs<ClassType>(type_id);
  139. if (!class_type) {
  140. break;
  141. }
  142. type_id = sem_ir.classes()
  143. .Get(class_type->class_id)
  144. .GetAdaptedType(sem_ir, class_type->specific_id);
  145. }
  146. return false;
  147. }
  148. } // namespace
  149. // Validates that this builtin has a signature matching the specified signature.
  150. //
  151. // `SignatureFnType` is a C++ function type that describes the signature that is
  152. // expected for this builtin. For example, `auto (AnyInt, AnyInt) -> AnyInt`
  153. // specifies that the builtin takes values of two integer types and returns a
  154. // value of a third integer type. Types used within the signature should provide
  155. // a `Check` function that validates that the Carbon type is expected:
  156. //
  157. // auto Check(const File&, ValidateState&, TypeId) -> bool;
  158. //
  159. // To constrain that the same type is used in multiple places in the signature,
  160. // `TypeParam<I, T>` can be used. For example:
  161. //
  162. // auto (TypeParam<0, AnyInt>, AnyInt) -> TypeParam<0, AnyInt>
  163. //
  164. // describes a builtin that takes two integers, and whose return type matches
  165. // its first parameter type. For convenience, typedefs for `TypeParam<I, T>`
  166. // are used in the descriptions of the builtins.
  167. template <typename SignatureFnType>
  168. static auto ValidateSignature(const File& sem_ir,
  169. llvm::ArrayRef<TypeId> arg_types,
  170. TypeId return_type) -> bool {
  171. using SignatureTraits = llvm::function_traits<SignatureFnType*>;
  172. ValidateState state;
  173. // Must have expected number of arguments.
  174. if (arg_types.size() != SignatureTraits::num_args) {
  175. return false;
  176. }
  177. // Argument types must match.
  178. if (![&]<size_t... Indexes>(std::index_sequence<Indexes...>) {
  179. return ((Check<typename SignatureTraits::template arg_t<Indexes>>(
  180. sem_ir, state, arg_types[Indexes])) &&
  181. ...);
  182. }(std::make_index_sequence<SignatureTraits::num_args>())) {
  183. return false;
  184. }
  185. // Result type must match.
  186. if (!Check<typename SignatureTraits::result_t>(sem_ir, state, return_type)) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. // Validates the signature for NoOp. This ignores all arguments, only validating
  192. // that the return type is compatible.
  193. static auto ValidateNoOpSignature(const File& sem_ir,
  194. llvm::ArrayRef<TypeId> /*arg_types*/,
  195. TypeId return_type) -> bool {
  196. ValidateState state;
  197. return Check<NoReturn>(sem_ir, state, return_type);
  198. }
  199. // Descriptions of builtin functions follow. For each builtin, a corresponding
  200. // `BuiltinInfo` constant is declared describing properties of that builtin.
  201. namespace BuiltinFunctionInfo {
  202. // Convenience name used in the builtin type signatures below for a first
  203. // generic type parameter that is constrained to be an integer type.
  204. using IntT = TypeParam<0, AnyInt>;
  205. // Convenience name used in the builtin type signatures below for a second
  206. // generic type parameter that is constrained to be an integer type.
  207. using IntU = TypeParam<1, AnyInt>;
  208. // Convenience name used in the builtin type signatures below for a first
  209. // generic type parameter that is constrained to be a sized integer type.
  210. using SizedIntT = TypeParam<0, AnySizedInt>;
  211. // Convenience name used in the builtin type signatures below for a second
  212. // generic type parameter that is constrained to be a sized integer type.
  213. using SizedIntU = TypeParam<1, AnySizedInt>;
  214. // Convenience name used in the builtin type signatures below for a first
  215. // generic type parameter that is constrained to be an float type.
  216. using FloatT = TypeParam<0, AnyFloat>;
  217. // Not a builtin function.
  218. constexpr BuiltinInfo None = {"", nullptr};
  219. constexpr BuiltinInfo NoOp = {"no_op", ValidateNoOpSignature};
  220. // Prints a single character.
  221. constexpr BuiltinInfo PrintChar = {
  222. "print.char", ValidateSignature<auto(AnySizedInt)->AnySizedInt>};
  223. // Prints an integer.
  224. constexpr BuiltinInfo PrintInt = {
  225. "print.int", ValidateSignature<auto(AnySizedInt)->NoReturn>};
  226. // Reads a single character from stdin.
  227. constexpr BuiltinInfo ReadChar = {"read.char",
  228. ValidateSignature<auto()->AnySizedInt>};
  229. // Returns the `Core.CharLiteral` type.
  230. constexpr BuiltinInfo CharLiteralMakeType = {"char_literal.make_type",
  231. ValidateSignature<auto()->Type>};
  232. // Returns the `Core.IntLiteral` type.
  233. constexpr BuiltinInfo IntLiteralMakeType = {"int_literal.make_type",
  234. ValidateSignature<auto()->Type>};
  235. // Returns the `iN` type.
  236. // TODO: Should we use a more specific type as the type of the bit width?
  237. constexpr BuiltinInfo IntMakeTypeSigned = {
  238. "int.make_type_signed", ValidateSignature<auto(AnyInt)->Type>};
  239. // Returns the `uN` type.
  240. constexpr BuiltinInfo IntMakeTypeUnsigned = {
  241. "int.make_type_unsigned", ValidateSignature<auto(AnyInt)->Type>};
  242. // Returns float types, such as `f64`. Currently only supports `f64`.
  243. constexpr BuiltinInfo FloatMakeType = {"float.make_type",
  244. ValidateSignature<auto(AnyInt)->Type>};
  245. // Returns the `bool` type.
  246. constexpr BuiltinInfo BoolMakeType = {"bool.make_type",
  247. ValidateSignature<auto()->Type>};
  248. // Converts between char types, with a diagnostic if the value doesn't fit.
  249. constexpr BuiltinInfo CharConvertChecked = {
  250. "char.convert_checked",
  251. ValidateSignature<auto(CharLiteral)->CharCompatible>};
  252. // Converts between integer types, truncating if necessary.
  253. constexpr BuiltinInfo IntConvert = {"int.convert",
  254. ValidateSignature<auto(AnyInt)->AnyInt>};
  255. // Converts between integer types, with a diagnostic if the value doesn't fit.
  256. constexpr BuiltinInfo IntConvertChecked = {
  257. "int.convert_checked", ValidateSignature<auto(AnyInt)->AnyInt>};
  258. // "int.snegate": integer negation.
  259. constexpr BuiltinInfo IntSNegate = {"int.snegate",
  260. ValidateSignature<auto(IntT)->IntT>};
  261. // "int.sadd": integer addition.
  262. constexpr BuiltinInfo IntSAdd = {"int.sadd",
  263. ValidateSignature<auto(IntT, IntT)->IntT>};
  264. // "int.ssub": integer subtraction.
  265. constexpr BuiltinInfo IntSSub = {"int.ssub",
  266. ValidateSignature<auto(IntT, IntT)->IntT>};
  267. // "int.smul": integer multiplication.
  268. constexpr BuiltinInfo IntSMul = {"int.smul",
  269. ValidateSignature<auto(IntT, IntT)->IntT>};
  270. // "int.sdiv": integer division.
  271. constexpr BuiltinInfo IntSDiv = {"int.sdiv",
  272. ValidateSignature<auto(IntT, IntT)->IntT>};
  273. // "int.smod": integer modulo.
  274. constexpr BuiltinInfo IntSMod = {"int.smod",
  275. ValidateSignature<auto(IntT, IntT)->IntT>};
  276. // "int.unegate": unsigned integer negation.
  277. constexpr BuiltinInfo IntUNegate = {
  278. "int.unegate", ValidateSignature<auto(SizedIntT)->SizedIntT>};
  279. // "int.uadd": unsigned integer addition.
  280. constexpr BuiltinInfo IntUAdd = {
  281. "int.uadd", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  282. // "int.usub": unsigned integer subtraction.
  283. constexpr BuiltinInfo IntUSub = {
  284. "int.usub", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  285. // "int.umul": unsigned integer multiplication.
  286. constexpr BuiltinInfo IntUMul = {
  287. "int.umul", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  288. // "int.udiv": unsigned integer division.
  289. constexpr BuiltinInfo IntUDiv = {
  290. "int.udiv", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  291. // "int.mod": integer modulo.
  292. constexpr BuiltinInfo IntUMod = {
  293. "int.umod", ValidateSignature<auto(SizedIntT, SizedIntT)->SizedIntT>};
  294. // "int.complement": integer bitwise complement.
  295. constexpr BuiltinInfo IntComplement = {"int.complement",
  296. ValidateSignature<auto(IntT)->IntT>};
  297. // "int.and": integer bitwise and.
  298. constexpr BuiltinInfo IntAnd = {"int.and",
  299. ValidateSignature<auto(IntT, IntT)->IntT>};
  300. // "int.or": integer bitwise or.
  301. constexpr BuiltinInfo IntOr = {"int.or",
  302. ValidateSignature<auto(IntT, IntT)->IntT>};
  303. // "int.xor": integer bitwise xor.
  304. constexpr BuiltinInfo IntXor = {"int.xor",
  305. ValidateSignature<auto(IntT, IntT)->IntT>};
  306. // "int.left_shift": integer left shift.
  307. constexpr BuiltinInfo IntLeftShift = {
  308. "int.left_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  309. // "int.right_shift": integer right shift.
  310. constexpr BuiltinInfo IntRightShift = {
  311. "int.right_shift", ValidateSignature<auto(IntT, IntU)->IntT>};
  312. // "int.sadd_assign": integer in-place addition.
  313. constexpr BuiltinInfo IntSAddAssign = {
  314. "int.sadd_assign",
  315. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  316. // "int.ssub_assign": integer in-place subtraction.
  317. constexpr BuiltinInfo IntSSubAssign = {
  318. "int.ssub_assign",
  319. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  320. // "int.smul_assign": integer in-place multiplication.
  321. constexpr BuiltinInfo IntSMulAssign = {
  322. "int.smul_assign",
  323. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  324. // "int.sdiv_assign": integer in-place division.
  325. constexpr BuiltinInfo IntSDivAssign = {
  326. "int.sdiv_assign",
  327. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  328. // "int.smod_assign": integer in-place modulo.
  329. constexpr BuiltinInfo IntSModAssign = {
  330. "int.smod_assign",
  331. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  332. // "int.uadd_assign": unsigned integer in-place addition.
  333. constexpr BuiltinInfo IntUAddAssign = {
  334. "int.uadd_assign",
  335. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  336. // "int.usub_assign": unsigned integer in-place subtraction.
  337. constexpr BuiltinInfo IntUSubAssign = {
  338. "int.usub_assign",
  339. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  340. // "int.umul_assign": unsigned integer in-place multiplication.
  341. constexpr BuiltinInfo IntUMulAssign = {
  342. "int.umul_assign",
  343. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  344. // "int.udiv_assign": unsigned integer in-place division.
  345. constexpr BuiltinInfo IntUDivAssign = {
  346. "int.udiv_assign",
  347. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  348. // "int.mod_assign": integer in-place modulo.
  349. constexpr BuiltinInfo IntUModAssign = {
  350. "int.umod_assign",
  351. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  352. // "int.and_assign": integer in-place bitwise and.
  353. constexpr BuiltinInfo IntAndAssign = {
  354. "int.and_assign",
  355. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  356. // "int.or_assign": integer in-place bitwise or.
  357. constexpr BuiltinInfo IntOrAssign = {
  358. "int.or_assign",
  359. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  360. // "int.xor_assign": integer in-place bitwise xor.
  361. constexpr BuiltinInfo IntXorAssign = {
  362. "int.xor_assign",
  363. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntT)->NoReturn>};
  364. // "int.left_shift_assign": integer in-place left shift.
  365. constexpr BuiltinInfo IntLeftShiftAssign = {
  366. "int.left_shift_assign",
  367. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntU)->NoReturn>};
  368. // "int.right_shift_assign": integer in-place right shift.
  369. constexpr BuiltinInfo IntRightShiftAssign = {
  370. "int.right_shift_assign",
  371. ValidateSignature<auto(PointerTo<SizedIntT>, SizedIntU)->NoReturn>};
  372. // "int.eq": integer equality comparison.
  373. constexpr BuiltinInfo IntEq = {"int.eq",
  374. ValidateSignature<auto(IntT, IntU)->Bool>};
  375. // "int.neq": integer non-equality comparison.
  376. constexpr BuiltinInfo IntNeq = {"int.neq",
  377. ValidateSignature<auto(IntT, IntU)->Bool>};
  378. // "int.less": integer less than comparison.
  379. constexpr BuiltinInfo IntLess = {"int.less",
  380. ValidateSignature<auto(IntT, IntU)->Bool>};
  381. // "int.less_eq": integer less than or equal comparison.
  382. constexpr BuiltinInfo IntLessEq = {"int.less_eq",
  383. ValidateSignature<auto(IntT, IntU)->Bool>};
  384. // "int.greater": integer greater than comparison.
  385. constexpr BuiltinInfo IntGreater = {"int.greater",
  386. ValidateSignature<auto(IntT, IntU)->Bool>};
  387. // "int.greater_eq": integer greater than or equal comparison.
  388. constexpr BuiltinInfo IntGreaterEq = {
  389. "int.greater_eq", ValidateSignature<auto(IntT, IntU)->Bool>};
  390. // "float.negate": float negation.
  391. constexpr BuiltinInfo FloatNegate = {"float.negate",
  392. ValidateSignature<auto(FloatT)->FloatT>};
  393. // "float.add": float addition.
  394. constexpr BuiltinInfo FloatAdd = {
  395. "float.add", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  396. // "float.sub": float subtraction.
  397. constexpr BuiltinInfo FloatSub = {
  398. "float.sub", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  399. // "float.mul": float multiplication.
  400. constexpr BuiltinInfo FloatMul = {
  401. "float.mul", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  402. // "float.div": float division.
  403. constexpr BuiltinInfo FloatDiv = {
  404. "float.div", ValidateSignature<auto(FloatT, FloatT)->FloatT>};
  405. // "float.eq": float equality comparison.
  406. constexpr BuiltinInfo FloatEq = {"float.eq",
  407. ValidateSignature<auto(FloatT, FloatT)->Bool>};
  408. // "float.neq": float non-equality comparison.
  409. constexpr BuiltinInfo FloatNeq = {
  410. "float.neq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  411. // "float.less": float less than comparison.
  412. constexpr BuiltinInfo FloatLess = {
  413. "float.less", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  414. // "float.less_eq": float less than or equal comparison.
  415. constexpr BuiltinInfo FloatLessEq = {
  416. "float.less_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  417. // "float.greater": float greater than comparison.
  418. constexpr BuiltinInfo FloatGreater = {
  419. "float.greater", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  420. // "float.greater_eq": float greater than or equal comparison.
  421. constexpr BuiltinInfo FloatGreaterEq = {
  422. "float.greater_eq", ValidateSignature<auto(FloatT, FloatT)->Bool>};
  423. // "bool.eq": bool equality comparison.
  424. constexpr BuiltinInfo BoolEq = {"bool.eq",
  425. ValidateSignature<auto(Bool, Bool)->Bool>};
  426. // "bool.neq": bool non-equality comparison.
  427. constexpr BuiltinInfo BoolNeq = {"bool.neq",
  428. ValidateSignature<auto(Bool, Bool)->Bool>};
  429. // "type.and": facet type combination.
  430. constexpr BuiltinInfo TypeAnd = {"type.and",
  431. ValidateSignature<auto(Type, Type)->Type>};
  432. } // namespace BuiltinFunctionInfo
  433. CARBON_DEFINE_ENUM_CLASS_NAMES(BuiltinFunctionKind) = {
  434. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  435. BuiltinFunctionInfo::Name.name,
  436. #include "toolchain/sem_ir/builtin_function_kind.def"
  437. };
  438. // Returns the builtin function kind with the given name, or None if the name
  439. // is unknown.
  440. auto BuiltinFunctionKind::ForBuiltinName(llvm::StringRef name)
  441. -> BuiltinFunctionKind {
  442. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  443. if (name == BuiltinFunctionInfo::Name.name) { \
  444. return BuiltinFunctionKind::Name; \
  445. }
  446. #include "toolchain/sem_ir/builtin_function_kind.def"
  447. return BuiltinFunctionKind::None;
  448. }
  449. auto BuiltinFunctionKind::IsValidType(const File& sem_ir,
  450. llvm::ArrayRef<TypeId> arg_types,
  451. TypeId return_type) const -> bool {
  452. static constexpr ValidateFn* ValidateFns[] = {
  453. #define CARBON_SEM_IR_BUILTIN_FUNCTION_KIND(Name) \
  454. BuiltinFunctionInfo::Name.validate,
  455. #include "toolchain/sem_ir/builtin_function_kind.def"
  456. };
  457. return ValidateFns[AsInt()](sem_ir, arg_types, return_type);
  458. }
  459. // Determines whether a builtin call involves an integer literal in its
  460. // arguments or return type. If so, for many builtins we want to treat the call
  461. // as being compile-time-only. This is because `Core.IntLiteral` has an empty
  462. // runtime representation, and a value of that type isn't necessarily a
  463. // compile-time constant, so an arbitrary runtime value of type
  464. // `Core.IntLiteral` may not have a value available for the builtin to use. For
  465. // example, given:
  466. //
  467. // var n: Core.IntLiteral() = 123;
  468. //
  469. // we would be unable to lower a runtime operation such as `(1 as i32) << n`
  470. // because the runtime representation of `n` doesn't track its value at all.
  471. //
  472. // For now, we treat all operations involving `Core.IntLiteral` as being
  473. // compile-time-only.
  474. //
  475. // TODO: We will need to accept things like `some_i32 << 5` eventually. We could
  476. // allow builtin calls at runtime if all the IntLiteral arguments have constant
  477. // values, or add logic to the prelude to promote the `IntLiteral` operand to a
  478. // different type in such cases.
  479. //
  480. // TODO: For now, we also treat builtins *returning* `Core.IntLiteral` as being
  481. // compile-time-only. This is mostly done for simplicity, but should probably be
  482. // revisited.
  483. static auto AnyIntLiteralTypes(const File& sem_ir,
  484. llvm::ArrayRef<InstId> arg_ids,
  485. TypeId return_type_id) -> bool {
  486. if (sem_ir.types().Is<IntLiteralType>(return_type_id)) {
  487. return true;
  488. }
  489. for (auto arg_id : arg_ids) {
  490. if (sem_ir.types().Is<IntLiteralType>(
  491. sem_ir.insts().Get(arg_id).type_id())) {
  492. return true;
  493. }
  494. }
  495. return false;
  496. }
  497. auto BuiltinFunctionKind::IsCompTimeOnly(const File& sem_ir,
  498. llvm::ArrayRef<InstId> arg_ids,
  499. TypeId return_type_id) const -> bool {
  500. switch (*this) {
  501. case CharConvertChecked:
  502. case IntConvertChecked:
  503. // Checked conversions are compile-time only.
  504. return true;
  505. case IntConvert:
  506. case IntSNegate:
  507. case IntComplement:
  508. case IntSAdd:
  509. case IntSSub:
  510. case IntSMul:
  511. case IntSDiv:
  512. case IntSMod:
  513. case IntAnd:
  514. case IntOr:
  515. case IntXor:
  516. case IntLeftShift:
  517. case IntRightShift:
  518. case IntEq:
  519. case IntNeq:
  520. case IntLess:
  521. case IntLessEq:
  522. case IntGreater:
  523. case IntGreaterEq:
  524. // Integer operations are compile-time-only if they involve integer
  525. // literal types. See AnyIntLiteralTypes comment for explanation.
  526. return AnyIntLiteralTypes(sem_ir, arg_ids, return_type_id);
  527. case TypeAnd:
  528. return true;
  529. default:
  530. // TODO: Should the sized MakeType functions be compile-time only? We
  531. // can't produce diagnostics for bad sizes at runtime.
  532. return false;
  533. }
  534. }
  535. } // namespace Carbon::SemIR