builtin_function_kind.cpp 22 KB

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