Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 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 | |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 15 | #include "Debug.hpp" |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 16 | #include "EmulatedIntrinsics.hpp" |
| 17 | #include "OptimalIntrinsics.hpp" |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 18 | #include "Print.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 19 | #include "Reactor.hpp" |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 20 | #include "ReactorDebugInfo.hpp" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 21 | |
Nicolas Capens | 1a3ce87 | 2018-10-10 10:42:36 -0400 | [diff] [blame] | 22 | #include "ExecutableMemory.hpp" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 23 | #include "Optimizer.hpp" |
Nicolas Capens | a062f32 | 2018-09-06 15:34:46 -0400 | [diff] [blame] | 24 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 25 | #include "src/IceCfg.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 26 | #include "src/IceCfgNode.h" |
| 27 | #include "src/IceELFObjectWriter.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 28 | #include "src/IceELFStreamer.h" |
| 29 | #include "src/IceGlobalContext.h" |
Nicolas Capens | 8dfd9a7 | 2016-10-13 17:44:51 -0400 | [diff] [blame] | 30 | #include "src/IceGlobalInits.h" |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 31 | #include "src/IceTypes.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 32 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 34 | #include "llvm/Support/FileSystem.h" |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame^] | 35 | #include "llvm/Support/ManagedStatic.h" |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 36 | #include "llvm/Support/raw_os_ostream.h" |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 37 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 38 | #include "marl/event.h" |
| 39 | |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 40 | #if __has_feature(memory_sanitizer) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 41 | # include <sanitizer/msan_interface.h> |
Nicolas Capens | 6a990f8 | 2018-07-06 15:54:07 -0400 | [diff] [blame] | 42 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 43 | |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 44 | #if defined(_WIN32) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 45 | # ifndef WIN32_LEAN_AND_MEAN |
| 46 | # define WIN32_LEAN_AND_MEAN |
| 47 | # endif // !WIN32_LEAN_AND_MEAN |
| 48 | # ifndef NOMINMAX |
| 49 | # define NOMINMAX |
| 50 | # endif // !NOMINMAX |
| 51 | # include <Windows.h> |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 52 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 53 | |
Ben Clayton | 683bad8 | 2020-02-10 23:57:09 +0000 | [diff] [blame] | 54 | #include <array> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 55 | #include <iostream> |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 56 | #include <limits> |
| 57 | #include <mutex> |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 58 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 59 | // Subzero utility functions |
| 60 | // These functions only accept and return Subzero (Ice) types, and do not access any globals. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 61 | namespace { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 62 | namespace sz { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 63 | |
| 64 | Ice::Cfg *createFunction(Ice::GlobalContext *context, Ice::Type returnType, const std::vector<Ice::Type> ¶mTypes) |
| 65 | { |
| 66 | uint32_t sequenceNumber = 0; |
| 67 | auto function = Ice::Cfg::create(context, sequenceNumber).release(); |
| 68 | |
| 69 | Ice::CfgLocalAllocatorScope allocScope{ function }; |
| 70 | |
| 71 | for(auto type : paramTypes) |
| 72 | { |
| 73 | Ice::Variable *arg = function->makeVariable(type); |
| 74 | function->addArg(arg); |
| 75 | } |
| 76 | |
| 77 | Ice::CfgNode *node = function->makeNode(); |
| 78 | function->setEntryNode(node); |
| 79 | |
| 80 | return function; |
| 81 | } |
| 82 | |
| 83 | Ice::Type getPointerType(Ice::Type elementType) |
| 84 | { |
| 85 | if(sizeof(void *) == 8) |
| 86 | { |
| 87 | return Ice::IceType_i64; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | return Ice::IceType_i32; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Ice::Variable *allocateStackVariable(Ice::Cfg *function, Ice::Type type, int arraySize = 0) |
| 96 | { |
| 97 | int typeSize = Ice::typeWidthInBytes(type); |
| 98 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 99 | |
| 100 | auto bytes = Ice::ConstantInteger32::create(function->getContext(), Ice::IceType_i32, totalSize); |
| 101 | auto address = function->makeVariable(getPointerType(type)); |
| 102 | auto alloca = Ice::InstAlloca::create(function, address, bytes, typeSize); |
| 103 | function->getEntryNode()->getInsts().push_front(alloca); |
| 104 | |
| 105 | return address; |
| 106 | } |
| 107 | |
| 108 | Ice::Constant *getConstantPointer(Ice::GlobalContext *context, void const *ptr) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 109 | { |
| 110 | if(sizeof(void *) == 8) |
| 111 | { |
| 112 | return context->getConstantInt64(reinterpret_cast<intptr_t>(ptr)); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | return context->getConstantInt32(reinterpret_cast<intptr_t>(ptr)); |
| 117 | } |
| 118 | } |
| 119 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 120 | // TODO(amaiorano): remove this prototype once these are moved to separate header/cpp |
| 121 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 122 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 123 | // Wrapper for calls on C functions with Ice types |
| 124 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, Ice::Operand *callTarget, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 125 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 126 | Ice::Variable *ret = nullptr; |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 127 | |
| 128 | // Subzero doesn't support boolean return values. Replace with an i32 temporarily, |
| 129 | // then truncate result to bool. |
| 130 | // TODO(b/151158858): Add support to Subzero's InstCall for bool-returning functions |
| 131 | const bool returningBool = (retTy == Ice::IceType_i1); |
| 132 | if(returningBool) |
| 133 | { |
| 134 | ret = function->makeVariable(Ice::IceType_i32); |
| 135 | } |
| 136 | else if(retTy != Ice::IceType_void) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 137 | { |
| 138 | ret = function->makeVariable(retTy); |
| 139 | } |
| 140 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 141 | auto call = Ice::InstCall::create(function, iceArgs.size(), ret, callTarget, false, false, isVariadic); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 142 | for(auto arg : iceArgs) |
| 143 | { |
| 144 | call->addArg(arg); |
| 145 | } |
| 146 | |
| 147 | basicBlock->appendInst(call); |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 148 | |
| 149 | if(returningBool) |
| 150 | { |
| 151 | // Truncate result to bool so that if any (lsb) bits were set, result will be true |
| 152 | ret = createTruncate(function, basicBlock, ret, Ice::IceType_i1); |
| 153 | } |
| 154 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 158 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Type retTy, void const *fptr, const std::vector<Ice::Operand *> &iceArgs, bool isVariadic) |
| 159 | { |
| 160 | Ice::Operand *callTarget = getConstantPointer(function->getContext(), fptr); |
| 161 | return Call(function, basicBlock, retTy, callTarget, iceArgs, isVariadic); |
| 162 | } |
| 163 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 164 | // Wrapper for calls on C functions with Ice types |
| 165 | template<typename Return, typename... CArgs, typename... RArgs> |
| 166 | Ice::Variable *Call(Ice::Cfg *function, Ice::CfgNode *basicBlock, Return(fptr)(CArgs...), RArgs &&... args) |
| 167 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 168 | static_assert(sizeof...(CArgs) == sizeof...(RArgs), "Expected number of args don't match"); |
| 169 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 170 | Ice::Type retTy = T(rr::CToReactorT<Return>::type()); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 171 | std::vector<Ice::Operand *> iceArgs{ std::forward<RArgs>(args)... }; |
Antonio Maiorano | ad3e42a | 2020-02-26 14:23:09 -0500 | [diff] [blame] | 172 | return Call(function, basicBlock, retTy, reinterpret_cast<void const *>(fptr), iceArgs, false); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 175 | // Returns a non-const variable copy of const v |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 176 | Ice::Variable *createUnconstCast(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Constant *v) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 177 | { |
| 178 | Ice::Variable *result = function->makeVariable(v->getType()); |
| 179 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Bitcast, result, v); |
| 180 | basicBlock->appendInst(cast); |
| 181 | return result; |
| 182 | } |
| 183 | |
Antonio Maiorano | 16ae92a | 2020-03-10 10:53:24 -0400 | [diff] [blame] | 184 | Ice::Variable *createTruncate(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *from, Ice::Type toType) |
| 185 | { |
| 186 | Ice::Variable *to = function->makeVariable(toType); |
| 187 | Ice::InstCast *cast = Ice::InstCast::create(function, Ice::InstCast::Trunc, to, from); |
| 188 | basicBlock->appendInst(cast); |
| 189 | return to; |
| 190 | } |
| 191 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 192 | Ice::Variable *createLoad(Ice::Cfg *function, Ice::CfgNode *basicBlock, Ice::Operand *ptr, Ice::Type type, unsigned int align) |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 193 | { |
| 194 | // TODO(b/148272103): InstLoad assumes that a constant ptr is an offset, rather than an |
| 195 | // absolute address. We circumvent this by casting to a non-const variable, and loading |
| 196 | // from that. |
| 197 | if(auto *cptr = llvm::dyn_cast<Ice::Constant>(ptr)) |
| 198 | { |
| 199 | ptr = sz::createUnconstCast(function, basicBlock, cptr); |
| 200 | } |
| 201 | |
| 202 | Ice::Variable *result = function->makeVariable(type); |
| 203 | auto load = Ice::InstLoad::create(function, result, ptr, align); |
| 204 | basicBlock->appendInst(load); |
| 205 | |
| 206 | return result; |
| 207 | } |
| 208 | |
| 209 | } // namespace sz |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 210 | } // namespace |
| 211 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 212 | namespace rr { |
| 213 | class ELFMemoryStreamer; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 214 | class CoroutineGenerator; |
| 215 | } // namespace rr |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 216 | |
| 217 | namespace { |
| 218 | |
Antonio Maiorano | 8b4cf1c | 2021-01-26 14:40:03 -0500 | [diff] [blame^] | 219 | // Used to automatically invoke llvm_shutdown() when driver is unloaded |
| 220 | llvm::llvm_shutdown_obj llvmShutdownObj; |
| 221 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 222 | // Default configuration settings. Must be accessed under mutex lock. |
| 223 | std::mutex defaultConfigLock; |
| 224 | rr::Config &defaultConfig() |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 225 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 226 | // This uses a static in a function to avoid the cost of a global static |
| 227 | // initializer. See http://neugierig.org/software/chromium/notes/2011/08/static-initializers.html |
| 228 | static rr::Config config = rr::Config::Edit() |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 229 | .apply({}); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 230 | return config; |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 233 | Ice::GlobalContext *context = nullptr; |
| 234 | Ice::Cfg *function = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 235 | Ice::CfgNode *entryBlock = nullptr; |
| 236 | Ice::CfgNode *basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 237 | Ice::CfgNode *basicBlock = nullptr; |
| 238 | Ice::CfgLocalAllocatorScope *allocator = nullptr; |
| 239 | rr::ELFMemoryStreamer *routine = nullptr; |
| 240 | |
| 241 | std::mutex codegenMutex; |
| 242 | |
| 243 | Ice::ELFFileStreamer *elfFile = nullptr; |
| 244 | Ice::Fdstream *out = nullptr; |
| 245 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 246 | // Coroutine globals |
| 247 | rr::Type *coroYieldType = nullptr; |
| 248 | std::shared_ptr<rr::CoroutineGenerator> coroGen; |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 249 | marl::Scheduler &getOrCreateScheduler() |
| 250 | { |
| 251 | static auto scheduler = [] { |
Ben Clayton | ef3914c | 2020-06-15 22:17:46 +0100 | [diff] [blame] | 252 | marl::Scheduler::Config cfg; |
| 253 | cfg.setWorkerThreadCount(8); |
| 254 | return std::make_unique<marl::Scheduler>(cfg); |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 255 | }(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 256 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 257 | return *scheduler; |
| 258 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 259 | } // Anonymous namespace |
| 260 | |
| 261 | namespace { |
| 262 | |
| 263 | #if !defined(__i386__) && defined(_M_IX86) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 264 | # define __i386__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 265 | #endif |
| 266 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 267 | #if !defined(__x86_64__) && (defined(_M_AMD64) || defined(_M_X64)) |
| 268 | # define __x86_64__ 1 |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 269 | #endif |
| 270 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 271 | Ice::OptLevel toIce(rr::Optimization::Level level) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 272 | { |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 273 | switch(level) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 274 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 275 | // Note that Opt_0 and Opt_1 are not implemented by Subzero |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 276 | case rr::Optimization::Level::None: return Ice::Opt_m1; |
| 277 | case rr::Optimization::Level::Less: return Ice::Opt_m1; |
| 278 | case rr::Optimization::Level::Default: return Ice::Opt_2; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 279 | case rr::Optimization::Level::Aggressive: return Ice::Opt_2; |
| 280 | default: UNREACHABLE("Unknown Optimization Level %d", int(level)); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 281 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 282 | return Ice::Opt_2; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 283 | } |
| 284 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 285 | Ice::Intrinsics::MemoryOrder stdToIceMemoryOrder(std::memory_order memoryOrder) |
| 286 | { |
| 287 | switch(memoryOrder) |
| 288 | { |
| 289 | case std::memory_order_relaxed: return Ice::Intrinsics::MemoryOrderRelaxed; |
| 290 | case std::memory_order_consume: return Ice::Intrinsics::MemoryOrderConsume; |
| 291 | case std::memory_order_acquire: return Ice::Intrinsics::MemoryOrderAcquire; |
| 292 | case std::memory_order_release: return Ice::Intrinsics::MemoryOrderRelease; |
| 293 | case std::memory_order_acq_rel: return Ice::Intrinsics::MemoryOrderAcquireRelease; |
| 294 | case std::memory_order_seq_cst: return Ice::Intrinsics::MemoryOrderSequentiallyConsistent; |
| 295 | } |
| 296 | return Ice::Intrinsics::MemoryOrderInvalid; |
| 297 | } |
| 298 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 299 | class CPUID |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 300 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 301 | public: |
| 302 | const static bool ARM; |
| 303 | const static bool SSE4_1; |
Nicolas Capens | 47dc867 | 2017-04-25 12:54:39 -0400 | [diff] [blame] | 304 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 305 | private: |
| 306 | static void cpuid(int registers[4], int info) |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 307 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 308 | #if defined(__i386__) || defined(__x86_64__) |
| 309 | # if defined(_WIN32) |
| 310 | __cpuid(registers, info); |
| 311 | # else |
| 312 | __asm volatile("cpuid" |
| 313 | : "=a"(registers[0]), "=b"(registers[1]), "=c"(registers[2]), "=d"(registers[3]) |
| 314 | : "a"(info)); |
| 315 | # endif |
| 316 | #else |
| 317 | registers[0] = 0; |
| 318 | registers[1] = 0; |
| 319 | registers[2] = 0; |
| 320 | registers[3] = 0; |
| 321 | #endif |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 324 | static bool detectARM() |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 325 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 326 | #if defined(__arm__) || defined(__aarch64__) |
| 327 | return true; |
| 328 | #elif defined(__i386__) || defined(__x86_64__) |
| 329 | return false; |
| 330 | #elif defined(__mips__) |
| 331 | return false; |
| 332 | #else |
| 333 | # error "Unknown architecture" |
| 334 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 335 | } |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 336 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 337 | static bool detectSSE4_1() |
| 338 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 339 | #if defined(__i386__) || defined(__x86_64__) |
| 340 | int registers[4]; |
| 341 | cpuid(registers, 1); |
| 342 | return (registers[2] & 0x00080000) != 0; |
| 343 | #else |
| 344 | return false; |
| 345 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 346 | } |
| 347 | }; |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 348 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 349 | const bool CPUID::ARM = CPUID::detectARM(); |
| 350 | const bool CPUID::SSE4_1 = CPUID::detectSSE4_1(); |
| 351 | const bool emulateIntrinsics = false; |
| 352 | const bool emulateMismatchedBitCast = CPUID::ARM; |
Nicolas Capens | f7b7588 | 2017-04-26 09:30:47 -0400 | [diff] [blame] | 353 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 354 | constexpr bool subzeroDumpEnabled = false; |
| 355 | constexpr bool subzeroEmitTextAsm = false; |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 356 | |
| 357 | #if !ALLOW_DUMP |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 358 | static_assert(!subzeroDumpEnabled, "Compile Subzero with ALLOW_DUMP=1 for subzeroDumpEnabled"); |
| 359 | static_assert(!subzeroEmitTextAsm, "Compile Subzero with ALLOW_DUMP=1 for subzeroEmitTextAsm"); |
Antonio Maiorano | 05ac79a | 2019-11-20 15:45:13 -0500 | [diff] [blame] | 360 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 361 | |
| 362 | } // anonymous namespace |
| 363 | |
| 364 | namespace rr { |
| 365 | |
Antonio Maiorano | ab210f9 | 2019-12-13 16:26:24 -0500 | [diff] [blame] | 366 | std::string BackendName() |
| 367 | { |
| 368 | return "Subzero"; |
| 369 | } |
| 370 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 371 | const Capabilities Caps = { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 372 | true, // CoroutinesSupported |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | enum EmulatedType |
| 376 | { |
| 377 | EmulatedShift = 16, |
| 378 | EmulatedV2 = 2 << EmulatedShift, |
| 379 | EmulatedV4 = 4 << EmulatedShift, |
| 380 | EmulatedV8 = 8 << EmulatedShift, |
| 381 | EmulatedBits = EmulatedV2 | EmulatedV4 | EmulatedV8, |
| 382 | |
| 383 | Type_v2i32 = Ice::IceType_v4i32 | EmulatedV2, |
| 384 | Type_v4i16 = Ice::IceType_v8i16 | EmulatedV4, |
| 385 | Type_v2i16 = Ice::IceType_v8i16 | EmulatedV2, |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 386 | Type_v8i8 = Ice::IceType_v16i8 | EmulatedV8, |
| 387 | Type_v4i8 = Ice::IceType_v16i8 | EmulatedV4, |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 388 | Type_v2f32 = Ice::IceType_v4f32 | EmulatedV2, |
| 389 | }; |
| 390 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 391 | class Value : public Ice::Operand |
| 392 | {}; |
| 393 | class SwitchCases : public Ice::InstSwitch |
| 394 | {}; |
| 395 | class BasicBlock : public Ice::CfgNode |
| 396 | {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 397 | |
| 398 | Ice::Type T(Type *t) |
| 399 | { |
| 400 | static_assert(static_cast<unsigned int>(Ice::IceType_NUM) < static_cast<unsigned int>(EmulatedBits), "Ice::Type overlaps with our emulated types!"); |
| 401 | return (Ice::Type)(reinterpret_cast<std::intptr_t>(t) & ~EmulatedBits); |
Nicolas Capens | ccd5ecb | 2017-01-14 12:52:55 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 404 | Type *T(Ice::Type t) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 405 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 406 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | Type *T(EmulatedType t) |
| 410 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 411 | return reinterpret_cast<Type *>(t); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 414 | std::vector<Ice::Type> T(const std::vector<Type *> &types) |
| 415 | { |
| 416 | std::vector<Ice::Type> result; |
| 417 | result.reserve(types.size()); |
| 418 | for(auto &t : types) |
| 419 | { |
| 420 | result.push_back(T(t)); |
| 421 | } |
| 422 | return result; |
| 423 | } |
| 424 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 425 | Value *V(Ice::Operand *v) |
| 426 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 427 | return reinterpret_cast<Value *>(v); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 430 | Ice::Operand *V(Value *v) |
| 431 | { |
Antonio Maiorano | 38c065d | 2020-01-30 09:51:37 -0500 | [diff] [blame] | 432 | return reinterpret_cast<Ice::Operand *>(v); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 433 | } |
| 434 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 435 | std::vector<Ice::Operand *> V(const std::vector<Value *> &values) |
| 436 | { |
| 437 | std::vector<Ice::Operand *> result; |
| 438 | result.reserve(values.size()); |
| 439 | for(auto &v : values) |
| 440 | { |
| 441 | result.push_back(V(v)); |
| 442 | } |
| 443 | return result; |
| 444 | } |
| 445 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 446 | BasicBlock *B(Ice::CfgNode *b) |
| 447 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 448 | return reinterpret_cast<BasicBlock *>(b); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static size_t typeSize(Type *type) |
| 452 | { |
| 453 | if(reinterpret_cast<std::intptr_t>(type) & EmulatedBits) |
Ben Clayton | c790416 | 2019-04-17 17:35:48 -0400 | [diff] [blame] | 454 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 455 | switch(reinterpret_cast<std::intptr_t>(type)) |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 456 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 457 | case Type_v2i32: return 8; |
| 458 | case Type_v4i16: return 8; |
| 459 | case Type_v2i16: return 4; |
| 460 | case Type_v8i8: return 8; |
| 461 | case Type_v4i8: return 4; |
| 462 | case Type_v2f32: return 8; |
| 463 | default: ASSERT(false); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | return Ice::typeWidthInBytes(T(type)); |
| 468 | } |
| 469 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 470 | static void finalizeFunction() |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 471 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 472 | // Create a return if none was added |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 473 | if(::basicBlock->getInsts().empty() || ::basicBlock->getInsts().back().getKind() != Ice::Inst::Ret) |
| 474 | { |
| 475 | Nucleus::createRetVoid(); |
| 476 | } |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 477 | |
| 478 | // Connect the entry block to the top of the initial basic block |
| 479 | auto br = Ice::InstBr::create(::function, ::basicBlockTop); |
| 480 | ::entryBlock->appendInst(br); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 483 | using ElfHeader = std::conditional<sizeof(void *) == 8, Elf64_Ehdr, Elf32_Ehdr>::type; |
| 484 | using SectionHeader = std::conditional<sizeof(void *) == 8, Elf64_Shdr, Elf32_Shdr>::type; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 485 | |
| 486 | inline const SectionHeader *sectionHeader(const ElfHeader *elfHeader) |
| 487 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 488 | return reinterpret_cast<const SectionHeader *>((intptr_t)elfHeader + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | inline const SectionHeader *elfSection(const ElfHeader *elfHeader, int index) |
| 492 | { |
| 493 | return §ionHeader(elfHeader)[index]; |
| 494 | } |
| 495 | |
| 496 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf32_Rel &relocation, const SectionHeader &relocationTable) |
| 497 | { |
| 498 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 499 | |
| 500 | uint32_t index = relocation.getSymbol(); |
| 501 | int table = relocationTable.sh_link; |
| 502 | void *symbolValue = nullptr; |
| 503 | |
| 504 | if(index != SHN_UNDEF) |
| 505 | { |
| 506 | if(table == SHN_UNDEF) return nullptr; |
| 507 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 508 | |
| 509 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 510 | if(index >= symtab_entries) |
| 511 | { |
| 512 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
| 513 | return nullptr; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 516 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 517 | Elf32_Sym &symbol = ((Elf32_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 518 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 584088c | 2017-01-26 16:05:18 -0800 | [diff] [blame] | 519 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 520 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 521 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 522 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 523 | symbolValue = reinterpret_cast<void *>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 524 | } |
| 525 | else |
| 526 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 527 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 528 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 529 | } |
| 530 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 531 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 532 | unaligned_ptr<int32_t> patchSite = (int32_t *)(address + relocation.r_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 533 | |
| 534 | if(CPUID::ARM) |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 535 | { |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 536 | switch(relocation.getType()) |
| 537 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 538 | case R_ARM_NONE: |
| 539 | // No relocation |
| 540 | break; |
| 541 | case R_ARM_MOVW_ABS_NC: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 542 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 543 | uint32_t thumb = 0; // Calls to Thumb code not supported. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 544 | uint32_t lo = (uint32_t)(intptr_t)symbolValue | thumb; |
| 545 | *patchSite = (*patchSite & 0xFFF0F000) | ((lo & 0xF000) << 4) | (lo & 0x0FFF); |
| 546 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 547 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 548 | case R_ARM_MOVT_ABS: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 549 | { |
| 550 | uint32_t hi = (uint32_t)(intptr_t)(symbolValue) >> 16; |
| 551 | *patchSite = (*patchSite & 0xFFF0F000) | ((hi & 0xF000) << 4) | (hi & 0x0FFF); |
| 552 | } |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 553 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 554 | default: |
| 555 | ASSERT(false && "Unsupported relocation type"); |
| 556 | return nullptr; |
Nicolas Capens | f110e4d | 2017-05-03 15:33:49 -0400 | [diff] [blame] | 557 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 558 | } |
| 559 | else |
| 560 | { |
| 561 | switch(relocation.getType()) |
| 562 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 563 | case R_386_NONE: |
| 564 | // No relocation |
| 565 | break; |
| 566 | case R_386_32: |
| 567 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite); |
| 568 | break; |
| 569 | case R_386_PC32: |
| 570 | *patchSite = (int32_t)((intptr_t)symbolValue + *patchSite - (intptr_t)patchSite); |
| 571 | break; |
| 572 | default: |
| 573 | ASSERT(false && "Unsupported relocation type"); |
| 574 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 575 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 576 | } |
| 577 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 578 | return symbolValue; |
| 579 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 580 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 581 | static void *relocateSymbol(const ElfHeader *elfHeader, const Elf64_Rela &relocation, const SectionHeader &relocationTable) |
| 582 | { |
| 583 | const SectionHeader *target = elfSection(elfHeader, relocationTable.sh_info); |
| 584 | |
| 585 | uint32_t index = relocation.getSymbol(); |
| 586 | int table = relocationTable.sh_link; |
| 587 | void *symbolValue = nullptr; |
| 588 | |
| 589 | if(index != SHN_UNDEF) |
| 590 | { |
| 591 | if(table == SHN_UNDEF) return nullptr; |
| 592 | const SectionHeader *symbolTable = elfSection(elfHeader, table); |
| 593 | |
| 594 | uint32_t symtab_entries = symbolTable->sh_size / symbolTable->sh_entsize; |
| 595 | if(index >= symtab_entries) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 596 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 597 | ASSERT(index < symtab_entries && "Symbol Index out of range"); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 598 | return nullptr; |
| 599 | } |
| 600 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 601 | intptr_t symbolAddress = (intptr_t)elfHeader + symbolTable->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 602 | Elf64_Sym &symbol = ((Elf64_Sym *)symbolAddress)[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 603 | uint16_t section = symbol.st_shndx; |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 604 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 605 | if(section != SHN_UNDEF && section < SHN_LORESERVE) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 606 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 607 | const SectionHeader *target = elfSection(elfHeader, symbol.st_shndx); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 608 | symbolValue = reinterpret_cast<void *>((intptr_t)elfHeader + symbol.st_value + target->sh_offset); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 609 | } |
| 610 | else |
| 611 | { |
| 612 | return nullptr; |
| 613 | } |
| 614 | } |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 615 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 616 | intptr_t address = (intptr_t)elfHeader + target->sh_offset; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 617 | unaligned_ptr<int32_t> patchSite32 = (int32_t *)(address + relocation.r_offset); |
| 618 | unaligned_ptr<int64_t> patchSite64 = (int64_t *)(address + relocation.r_offset); |
Nicolas Capens | 6647836 | 2016-10-13 15:36:36 -0400 | [diff] [blame] | 619 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 620 | switch(relocation.getType()) |
| 621 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 622 | case R_X86_64_NONE: |
| 623 | // No relocation |
| 624 | break; |
| 625 | case R_X86_64_64: |
| 626 | *patchSite64 = (int64_t)((intptr_t)symbolValue + *patchSite64 + relocation.r_addend); |
| 627 | break; |
| 628 | case R_X86_64_PC32: |
| 629 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 - (intptr_t)patchSite32 + relocation.r_addend); |
| 630 | break; |
| 631 | case R_X86_64_32S: |
| 632 | *patchSite32 = (int32_t)((intptr_t)symbolValue + *patchSite32 + relocation.r_addend); |
| 633 | break; |
| 634 | default: |
| 635 | ASSERT(false && "Unsupported relocation type"); |
| 636 | return nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | return symbolValue; |
| 640 | } |
| 641 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 642 | struct EntryPoint |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 643 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 644 | const void *entry; |
| 645 | size_t codeSize = 0; |
| 646 | }; |
| 647 | |
| 648 | std::vector<EntryPoint> loadImage(uint8_t *const elfImage, const std::vector<const char *> &functionNames) |
| 649 | { |
| 650 | ASSERT(functionNames.size() > 0); |
| 651 | std::vector<EntryPoint> entryPoints(functionNames.size()); |
| 652 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 653 | ElfHeader *elfHeader = (ElfHeader *)elfImage; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 654 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 655 | // TODO: assert? |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 656 | if(!elfHeader->checkMagic()) |
| 657 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 658 | return {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // Expect ELF bitness to match platform |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 662 | ASSERT(sizeof(void *) == 8 ? elfHeader->getFileClass() == ELFCLASS64 : elfHeader->getFileClass() == ELFCLASS32); |
| 663 | #if defined(__i386__) |
| 664 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_386); |
| 665 | #elif defined(__x86_64__) |
| 666 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_X86_64); |
| 667 | #elif defined(__arm__) |
| 668 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_ARM); |
| 669 | #elif defined(__aarch64__) |
| 670 | ASSERT(sizeof(void *) == 8 && elfHeader->e_machine == EM_AARCH64); |
| 671 | #elif defined(__mips__) |
| 672 | ASSERT(sizeof(void *) == 4 && elfHeader->e_machine == EM_MIPS); |
| 673 | #else |
| 674 | # error "Unsupported platform" |
| 675 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 676 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 677 | SectionHeader *sectionHeader = (SectionHeader *)(elfImage + elfHeader->e_shoff); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 678 | |
| 679 | for(int i = 0; i < elfHeader->e_shnum; i++) |
| 680 | { |
| 681 | if(sectionHeader[i].sh_type == SHT_PROGBITS) |
| 682 | { |
| 683 | if(sectionHeader[i].sh_flags & SHF_EXECINSTR) |
| 684 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 685 | auto findSectionNameEntryIndex = [&]() -> size_t { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 686 | auto sectionNameOffset = sectionHeader[elfHeader->e_shstrndx].sh_offset + sectionHeader[i].sh_name; |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 687 | const char *sectionName = reinterpret_cast<const char *>(elfImage + sectionNameOffset); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 688 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 689 | for(size_t j = 0; j < functionNames.size(); ++j) |
| 690 | { |
| 691 | if(strstr(sectionName, functionNames[j]) != nullptr) |
| 692 | { |
| 693 | return j; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | UNREACHABLE("Failed to find executable section that matches input function names"); |
| 698 | return static_cast<size_t>(-1); |
| 699 | }; |
| 700 | |
| 701 | size_t index = findSectionNameEntryIndex(); |
| 702 | entryPoints[index].entry = elfImage + sectionHeader[i].sh_offset; |
| 703 | entryPoints[index].codeSize = sectionHeader[i].sh_size; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 704 | } |
| 705 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 706 | else if(sectionHeader[i].sh_type == SHT_REL) |
| 707 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 708 | ASSERT(sizeof(void *) == 4 && "UNIMPLEMENTED"); // Only expected/implemented for 32-bit code |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 709 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 710 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 711 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 712 | const Elf32_Rel &relocation = ((const Elf32_Rel *)(elfImage + sectionHeader[i].sh_offset))[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 713 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 714 | } |
| 715 | } |
| 716 | else if(sectionHeader[i].sh_type == SHT_RELA) |
| 717 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 718 | ASSERT(sizeof(void *) == 8 && "UNIMPLEMENTED"); // Only expected/implemented for 64-bit code |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 719 | |
| 720 | for(Elf32_Word index = 0; index < sectionHeader[i].sh_size / sectionHeader[i].sh_entsize; index++) |
| 721 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 722 | const Elf64_Rela &relocation = ((const Elf64_Rela *)(elfImage + sectionHeader[i].sh_offset))[index]; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 723 | relocateSymbol(elfHeader, relocation, sectionHeader[i]); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 728 | return entryPoints; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | template<typename T> |
| 732 | struct ExecutableAllocator |
| 733 | { |
| 734 | ExecutableAllocator() {} |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 735 | template<class U> |
| 736 | ExecutableAllocator(const ExecutableAllocator<U> &other) |
| 737 | {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 738 | |
| 739 | using value_type = T; |
| 740 | using size_type = std::size_t; |
| 741 | |
| 742 | T *allocate(size_type n) |
| 743 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 744 | return (T *)allocateMemoryPages( |
| 745 | sizeof(T) * n, PERMISSION_READ | PERMISSION_WRITE, true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | void deallocate(T *p, size_type n) |
| 749 | { |
Sergey Ulanov | ebb0bec | 2019-12-12 11:53:04 -0800 | [diff] [blame] | 750 | deallocateMemoryPages(p, sizeof(T) * n); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 751 | } |
| 752 | }; |
| 753 | |
| 754 | class ELFMemoryStreamer : public Ice::ELFStreamer, public Routine |
| 755 | { |
| 756 | ELFMemoryStreamer(const ELFMemoryStreamer &) = delete; |
| 757 | ELFMemoryStreamer &operator=(const ELFMemoryStreamer &) = delete; |
| 758 | |
| 759 | public: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 760 | ELFMemoryStreamer() |
| 761 | : Routine() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 762 | { |
| 763 | position = 0; |
| 764 | buffer.reserve(0x1000); |
| 765 | } |
| 766 | |
| 767 | ~ELFMemoryStreamer() override |
| 768 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void write8(uint8_t Value) override |
| 772 | { |
| 773 | if(position == (uint64_t)buffer.size()) |
| 774 | { |
| 775 | buffer.push_back(Value); |
| 776 | position++; |
| 777 | } |
| 778 | else if(position < (uint64_t)buffer.size()) |
| 779 | { |
| 780 | buffer[position] = Value; |
| 781 | position++; |
| 782 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 783 | else |
| 784 | ASSERT(false && "UNIMPLEMENTED"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void writeBytes(llvm::StringRef Bytes) override |
| 788 | { |
| 789 | std::size_t oldSize = buffer.size(); |
| 790 | buffer.resize(oldSize + Bytes.size()); |
| 791 | memcpy(&buffer[oldSize], Bytes.begin(), Bytes.size()); |
| 792 | position += Bytes.size(); |
| 793 | } |
| 794 | |
| 795 | uint64_t tell() const override { return position; } |
| 796 | |
| 797 | void seek(uint64_t Off) override { position = Off; } |
| 798 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 799 | std::vector<EntryPoint> loadImageAndGetEntryPoints(const std::vector<const char *> &functionNames) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 800 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 801 | auto entryPoints = loadImage(&buffer[0], functionNames); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 802 | |
| 803 | #if defined(_WIN32) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 804 | FlushInstructionCache(GetCurrentProcess(), NULL, 0); |
| 805 | #else |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 806 | for(auto &entryPoint : entryPoints) |
| 807 | { |
| 808 | __builtin___clear_cache((char *)entryPoint.entry, (char *)entryPoint.entry + entryPoint.codeSize); |
| 809 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 810 | #endif |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 811 | |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 812 | return entryPoints; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 813 | } |
| 814 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 815 | void finalize() |
| 816 | { |
| 817 | position = std::numeric_limits<std::size_t>::max(); // Can't stream more data after this |
| 818 | |
| 819 | protectMemoryPages(&buffer[0], buffer.size(), PERMISSION_READ | PERMISSION_EXECUTE); |
| 820 | } |
| 821 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 822 | void setEntry(int index, const void *func) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 823 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 824 | ASSERT(func); |
| 825 | funcs[index] = func; |
| 826 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 827 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 828 | const void *getEntry(int index) const override |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 829 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 830 | ASSERT(funcs[index]); |
| 831 | return funcs[index]; |
| 832 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 833 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 834 | const void *addConstantData(const void *data, size_t size, size_t alignment = 1) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 835 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 836 | // TODO(b/148086935): Replace with a buffer allocator. |
| 837 | size_t space = size + alignment; |
| 838 | auto buf = std::unique_ptr<uint8_t[]>(new uint8_t[space]); |
| 839 | void *ptr = buf.get(); |
| 840 | void *alignedPtr = std::align(alignment, size, ptr, space); |
| 841 | ASSERT(alignedPtr); |
| 842 | memcpy(alignedPtr, data, size); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 843 | constantData.emplace_back(std::move(buf)); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 844 | return alignedPtr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 845 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 846 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 847 | private: |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 848 | std::array<const void *, Nucleus::CoroutineEntryCount> funcs = {}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 849 | std::vector<uint8_t, ExecutableAllocator<uint8_t>> buffer; |
| 850 | std::size_t position; |
| 851 | std::vector<std::unique_ptr<uint8_t[]>> constantData; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 852 | }; |
| 853 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 854 | #ifdef ENABLE_RR_PRINT |
| 855 | void VPrintf(const std::vector<Value *> &vals) |
| 856 | { |
Antonio Maiorano | 8cbee41 | 2020-06-10 15:59:20 -0400 | [diff] [blame] | 857 | sz::Call(::function, ::basicBlock, Ice::IceType_i32, reinterpret_cast<const void *>(rr::DebugPrintf), V(vals), true); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 858 | } |
| 859 | #endif // ENABLE_RR_PRINT |
| 860 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 861 | Nucleus::Nucleus() |
| 862 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 863 | ::codegenMutex.lock(); // SubzeroReactor is currently not thread safe |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 864 | |
| 865 | Ice::ClFlags &Flags = Ice::ClFlags::Flags; |
| 866 | Ice::ClFlags::getParsedClFlags(Flags); |
| 867 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 868 | #if defined(__arm__) |
| 869 | Flags.setTargetArch(Ice::Target_ARM32); |
| 870 | Flags.setTargetInstructionSet(Ice::ARM32InstructionSet_HWDivArm); |
| 871 | #elif defined(__mips__) |
| 872 | Flags.setTargetArch(Ice::Target_MIPS32); |
| 873 | Flags.setTargetInstructionSet(Ice::BaseInstructionSet); |
| 874 | #else // x86 |
| 875 | Flags.setTargetArch(sizeof(void *) == 8 ? Ice::Target_X8664 : Ice::Target_X8632); |
| 876 | Flags.setTargetInstructionSet(CPUID::SSE4_1 ? Ice::X86InstructionSet_SSE4_1 : Ice::X86InstructionSet_SSE2); |
| 877 | #endif |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 878 | Flags.setOutFileType(Ice::FT_Elf); |
| 879 | Flags.setOptLevel(toIce(getDefaultConfig().getOptimization().getLevel())); |
| 880 | Flags.setApplicationBinaryInterface(Ice::ABI_Platform); |
| 881 | Flags.setVerbose(subzeroDumpEnabled ? Ice::IceV_Most : Ice::IceV_None); |
| 882 | Flags.setDisableHybridAssembly(true); |
| 883 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 884 | // Emit functions into separate sections in the ELF so we can find them by name |
| 885 | Flags.setFunctionSections(true); |
| 886 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 887 | static llvm::raw_os_ostream cout(std::cout); |
| 888 | static llvm::raw_os_ostream cerr(std::cerr); |
| 889 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 890 | if(subzeroEmitTextAsm) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 891 | { |
| 892 | // Decorate text asm with liveness info |
| 893 | Flags.setDecorateAsm(true); |
| 894 | } |
| 895 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 896 | if(false) // Write out to a file |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 897 | { |
| 898 | std::error_code errorCode; |
| 899 | ::out = new Ice::Fdstream("out.o", errorCode, llvm::sys::fs::F_None); |
| 900 | ::elfFile = new Ice::ELFFileStreamer(*out); |
| 901 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfFile); |
| 902 | } |
| 903 | else |
| 904 | { |
| 905 | ELFMemoryStreamer *elfMemory = new ELFMemoryStreamer(); |
| 906 | ::context = new Ice::GlobalContext(&cout, &cout, &cerr, elfMemory); |
| 907 | ::routine = elfMemory; |
| 908 | } |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 909 | |
Nicolas Capens | 00c30ce | 2020-10-29 09:17:25 -0400 | [diff] [blame] | 910 | #if !__has_feature(memory_sanitizer) |
| 911 | // thread_local variables in shared libraries are initialized at load-time, |
| 912 | // but this is not observed by MemorySanitizer if the loader itself was not |
| 913 | // instrumented, leading to false-positive unitialized variable errors. |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 914 | ASSERT(Variable::unmaterializedVariables == nullptr); |
Nicolas Capens | 46485a0 | 2020-06-17 01:31:10 -0400 | [diff] [blame] | 915 | #endif |
Antonio Maiorano | f14f6c4 | 2020-11-03 16:34:35 -0500 | [diff] [blame] | 916 | Variable::unmaterializedVariables = new Variable::UnmaterializedVariables{}; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | Nucleus::~Nucleus() |
| 920 | { |
Nicolas Capens | 7d6b591 | 2020-04-28 15:57:57 -0400 | [diff] [blame] | 921 | delete Variable::unmaterializedVariables; |
| 922 | Variable::unmaterializedVariables = nullptr; |
| 923 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 924 | delete ::routine; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 925 | ::routine = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 926 | |
| 927 | delete ::allocator; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 928 | ::allocator = nullptr; |
| 929 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 930 | delete ::function; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 931 | ::function = nullptr; |
| 932 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 933 | delete ::context; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 934 | ::context = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 935 | |
| 936 | delete ::elfFile; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 937 | ::elfFile = nullptr; |
| 938 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 939 | delete ::out; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 940 | ::out = nullptr; |
| 941 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 942 | ::entryBlock = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 943 | ::basicBlock = nullptr; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 944 | ::basicBlockTop = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 945 | |
| 946 | ::codegenMutex.unlock(); |
| 947 | } |
| 948 | |
| 949 | void Nucleus::setDefaultConfig(const Config &cfg) |
| 950 | { |
| 951 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 952 | ::defaultConfig() = cfg; |
| 953 | } |
| 954 | |
| 955 | void Nucleus::adjustDefaultConfig(const Config::Edit &cfgEdit) |
| 956 | { |
| 957 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 958 | auto &config = ::defaultConfig(); |
| 959 | config = cfgEdit.apply(config); |
| 960 | } |
| 961 | |
| 962 | Config Nucleus::getDefaultConfig() |
| 963 | { |
| 964 | std::unique_lock<std::mutex> lock(::defaultConfigLock); |
| 965 | return ::defaultConfig(); |
| 966 | } |
| 967 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 968 | // This function lowers and produces executable binary code in memory for the input functions, |
| 969 | // and returns a Routine with the entry points to these functions. |
| 970 | template<size_t Count> |
| 971 | static std::shared_ptr<Routine> acquireRoutine(Ice::Cfg *const (&functions)[Count], const char *const (&names)[Count], const Config::Edit &cfgEdit) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 972 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 973 | // This logic is modeled after the IceCompiler, as well as GlobalContext::translateFunctions |
| 974 | // and GlobalContext::emitItems. |
| 975 | |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 976 | if(subzeroDumpEnabled) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 977 | { |
| 978 | // Output dump strings immediately, rather than once buffer is full. Useful for debugging. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 979 | ::context->getStrDump().SetUnbuffered(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | ::context->emitFileHeader(); |
| 983 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 984 | // Translate |
| 985 | |
| 986 | for(size_t i = 0; i < Count; ++i) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 987 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 988 | Ice::Cfg *currFunc = functions[i]; |
| 989 | |
| 990 | // Install function allocator in TLS for Cfg-specific container allocators |
| 991 | Ice::CfgLocalAllocatorScope allocScope(currFunc); |
| 992 | |
| 993 | currFunc->setFunctionName(Ice::GlobalString::createWithString(::context, names[i])); |
| 994 | |
| 995 | rr::optimize(currFunc); |
| 996 | |
| 997 | currFunc->computeInOutEdges(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 998 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 999 | |
| 1000 | currFunc->translate(); |
Antonio Maiorano | b3d9a2a | 2020-02-14 14:38:04 -0500 | [diff] [blame] | 1001 | ASSERT_MSG(!currFunc->hasError(), "%s", currFunc->getError().c_str()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1002 | |
| 1003 | currFunc->getAssembler<>()->setInternal(currFunc->getInternal()); |
| 1004 | |
| 1005 | if(subzeroEmitTextAsm) |
| 1006 | { |
| 1007 | currFunc->emit(); |
| 1008 | } |
| 1009 | |
| 1010 | currFunc->emitIAS(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1013 | // Emit items |
| 1014 | |
| 1015 | ::context->lowerGlobals(""); |
| 1016 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1017 | auto objectWriter = ::context->getObjectWriter(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1018 | |
| 1019 | for(size_t i = 0; i < Count; ++i) |
| 1020 | { |
| 1021 | Ice::Cfg *currFunc = functions[i]; |
| 1022 | |
| 1023 | // Accumulate globals from functions to emit into the "last" section at the end |
| 1024 | auto globals = currFunc->getGlobalInits(); |
| 1025 | if(globals && !globals->empty()) |
| 1026 | { |
| 1027 | ::context->getGlobals()->merge(globals.get()); |
| 1028 | } |
| 1029 | |
| 1030 | auto assembler = currFunc->releaseAssembler(); |
| 1031 | assembler->alignFunction(); |
| 1032 | objectWriter->writeFunctionCode(currFunc->getFunctionName(), currFunc->getInternal(), assembler.get()); |
| 1033 | } |
| 1034 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1035 | ::context->lowerGlobals("last"); |
| 1036 | ::context->lowerConstants(); |
| 1037 | ::context->lowerJumpTables(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1038 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1039 | objectWriter->setUndefinedSyms(::context->getConstantExternSyms()); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1040 | ::context->emitTargetRODataSections(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1041 | objectWriter->writeNonUserSections(); |
| 1042 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1043 | // Done compiling functions, get entry pointers to each of them |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1044 | auto entryPoints = ::routine->loadImageAndGetEntryPoints({ names, names + Count }); |
| 1045 | ASSERT(entryPoints.size() == Count); |
| 1046 | for(size_t i = 0; i < entryPoints.size(); ++i) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1047 | { |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 1048 | ::routine->setEntry(i, entryPoints[i].entry); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | ::routine->finalize(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1052 | |
| 1053 | Routine *handoffRoutine = ::routine; |
| 1054 | ::routine = nullptr; |
| 1055 | |
| 1056 | return std::shared_ptr<Routine>(handoffRoutine); |
| 1057 | } |
| 1058 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1059 | std::shared_ptr<Routine> Nucleus::acquireRoutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 1060 | { |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1061 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1062 | return rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 1063 | } |
| 1064 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1065 | Value *Nucleus::allocateStackVariable(Type *t, int arraySize) |
| 1066 | { |
| 1067 | Ice::Type type = T(t); |
| 1068 | int typeSize = Ice::typeWidthInBytes(type); |
| 1069 | int totalSize = typeSize * (arraySize ? arraySize : 1); |
| 1070 | |
| 1071 | auto bytes = Ice::ConstantInteger32::create(::context, Ice::IceType_i32, totalSize); |
| 1072 | auto address = ::function->makeVariable(T(getPointerType(t))); |
| 1073 | auto alloca = Ice::InstAlloca::create(::function, address, bytes, typeSize); |
| 1074 | ::function->getEntryNode()->getInsts().push_front(alloca); |
| 1075 | |
| 1076 | return V(address); |
| 1077 | } |
| 1078 | |
| 1079 | BasicBlock *Nucleus::createBasicBlock() |
| 1080 | { |
| 1081 | return B(::function->makeNode()); |
| 1082 | } |
| 1083 | |
| 1084 | BasicBlock *Nucleus::getInsertBlock() |
| 1085 | { |
| 1086 | return B(::basicBlock); |
| 1087 | } |
| 1088 | |
| 1089 | void Nucleus::setInsertBlock(BasicBlock *basicBlock) |
| 1090 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1091 | // ASSERT(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1092 | |
| 1093 | Variable::materializeAll(); |
| 1094 | |
| 1095 | ::basicBlock = basicBlock; |
| 1096 | } |
| 1097 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1098 | void Nucleus::createFunction(Type *returnType, const std::vector<Type *> ¶mTypes) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1099 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1100 | ASSERT(::function == nullptr); |
| 1101 | ASSERT(::allocator == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1102 | ASSERT(::entryBlock == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1103 | ASSERT(::basicBlock == nullptr); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1104 | ASSERT(::basicBlockTop == nullptr); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1105 | |
| 1106 | ::function = sz::createFunction(::context, T(returnType), T(paramTypes)); |
| 1107 | |
| 1108 | // NOTE: The scoped allocator sets the TLS allocator to the one in the function. This global one |
| 1109 | // becomes invalid if another one is created; for example, when creating await and destroy functions |
| 1110 | // for coroutines, in which case, we must make sure to create a new scoped allocator for ::function again. |
| 1111 | // TODO: Get rid of this as a global, and create scoped allocs in every Nucleus function instead. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1112 | ::allocator = new Ice::CfgLocalAllocatorScope(::function); |
| 1113 | |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 1114 | ::entryBlock = ::function->getEntryNode(); |
| 1115 | ::basicBlock = ::function->makeNode(); |
| 1116 | ::basicBlockTop = ::basicBlock; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | Value *Nucleus::getArgument(unsigned int index) |
| 1120 | { |
| 1121 | return V(::function->getArgs()[index]); |
| 1122 | } |
| 1123 | |
| 1124 | void Nucleus::createRetVoid() |
| 1125 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1126 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1127 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1128 | // Code generated after this point is unreachable, so any variables |
| 1129 | // being read can safely return an undefined value. We have to avoid |
| 1130 | // materializing variables after the terminator ret instruction. |
| 1131 | Variable::killUnmaterialized(); |
| 1132 | |
| 1133 | Ice::InstRet *ret = Ice::InstRet::create(::function); |
| 1134 | ::basicBlock->appendInst(ret); |
| 1135 | } |
| 1136 | |
| 1137 | void Nucleus::createRet(Value *v) |
| 1138 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1139 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 1140 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1141 | // Code generated after this point is unreachable, so any variables |
| 1142 | // being read can safely return an undefined value. We have to avoid |
| 1143 | // materializing variables after the terminator ret instruction. |
| 1144 | Variable::killUnmaterialized(); |
| 1145 | |
| 1146 | Ice::InstRet *ret = Ice::InstRet::create(::function, v); |
| 1147 | ::basicBlock->appendInst(ret); |
| 1148 | } |
| 1149 | |
| 1150 | void Nucleus::createBr(BasicBlock *dest) |
| 1151 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1152 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1153 | Variable::materializeAll(); |
| 1154 | |
| 1155 | auto br = Ice::InstBr::create(::function, dest); |
| 1156 | ::basicBlock->appendInst(br); |
| 1157 | } |
| 1158 | |
| 1159 | void Nucleus::createCondBr(Value *cond, BasicBlock *ifTrue, BasicBlock *ifFalse) |
| 1160 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1161 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1162 | Variable::materializeAll(); |
| 1163 | |
| 1164 | auto br = Ice::InstBr::create(::function, cond, ifTrue, ifFalse); |
| 1165 | ::basicBlock->appendInst(br); |
| 1166 | } |
| 1167 | |
| 1168 | static bool isCommutative(Ice::InstArithmetic::OpKind op) |
| 1169 | { |
| 1170 | switch(op) |
| 1171 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1172 | case Ice::InstArithmetic::Add: |
| 1173 | case Ice::InstArithmetic::Fadd: |
| 1174 | case Ice::InstArithmetic::Mul: |
| 1175 | case Ice::InstArithmetic::Fmul: |
| 1176 | case Ice::InstArithmetic::And: |
| 1177 | case Ice::InstArithmetic::Or: |
| 1178 | case Ice::InstArithmetic::Xor: |
| 1179 | return true; |
| 1180 | default: |
| 1181 | return false; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | static Value *createArithmetic(Ice::InstArithmetic::OpKind op, Value *lhs, Value *rhs) |
| 1186 | { |
| 1187 | ASSERT(lhs->getType() == rhs->getType() || llvm::isa<Ice::Constant>(rhs)); |
| 1188 | |
| 1189 | bool swapOperands = llvm::isa<Ice::Constant>(lhs) && isCommutative(op); |
| 1190 | |
| 1191 | Ice::Variable *result = ::function->makeVariable(lhs->getType()); |
| 1192 | Ice::InstArithmetic *arithmetic = Ice::InstArithmetic::create(::function, op, result, swapOperands ? rhs : lhs, swapOperands ? lhs : rhs); |
| 1193 | ::basicBlock->appendInst(arithmetic); |
| 1194 | |
| 1195 | return V(result); |
| 1196 | } |
| 1197 | |
| 1198 | Value *Nucleus::createAdd(Value *lhs, Value *rhs) |
| 1199 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1200 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1201 | return createArithmetic(Ice::InstArithmetic::Add, lhs, rhs); |
| 1202 | } |
| 1203 | |
| 1204 | Value *Nucleus::createSub(Value *lhs, Value *rhs) |
| 1205 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1206 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1207 | return createArithmetic(Ice::InstArithmetic::Sub, lhs, rhs); |
| 1208 | } |
| 1209 | |
| 1210 | Value *Nucleus::createMul(Value *lhs, Value *rhs) |
| 1211 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1212 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1213 | return createArithmetic(Ice::InstArithmetic::Mul, lhs, rhs); |
| 1214 | } |
| 1215 | |
| 1216 | Value *Nucleus::createUDiv(Value *lhs, Value *rhs) |
| 1217 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1218 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1219 | return createArithmetic(Ice::InstArithmetic::Udiv, lhs, rhs); |
| 1220 | } |
| 1221 | |
| 1222 | Value *Nucleus::createSDiv(Value *lhs, Value *rhs) |
| 1223 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1224 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1225 | return createArithmetic(Ice::InstArithmetic::Sdiv, lhs, rhs); |
| 1226 | } |
| 1227 | |
| 1228 | Value *Nucleus::createFAdd(Value *lhs, Value *rhs) |
| 1229 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1230 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1231 | return createArithmetic(Ice::InstArithmetic::Fadd, lhs, rhs); |
| 1232 | } |
| 1233 | |
| 1234 | Value *Nucleus::createFSub(Value *lhs, Value *rhs) |
| 1235 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1236 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1237 | return createArithmetic(Ice::InstArithmetic::Fsub, lhs, rhs); |
| 1238 | } |
| 1239 | |
| 1240 | Value *Nucleus::createFMul(Value *lhs, Value *rhs) |
| 1241 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1242 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1243 | return createArithmetic(Ice::InstArithmetic::Fmul, lhs, rhs); |
| 1244 | } |
| 1245 | |
| 1246 | Value *Nucleus::createFDiv(Value *lhs, Value *rhs) |
| 1247 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1248 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1249 | return createArithmetic(Ice::InstArithmetic::Fdiv, lhs, rhs); |
| 1250 | } |
| 1251 | |
| 1252 | Value *Nucleus::createURem(Value *lhs, Value *rhs) |
| 1253 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1254 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1255 | return createArithmetic(Ice::InstArithmetic::Urem, lhs, rhs); |
| 1256 | } |
| 1257 | |
| 1258 | Value *Nucleus::createSRem(Value *lhs, Value *rhs) |
| 1259 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1260 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1261 | return createArithmetic(Ice::InstArithmetic::Srem, lhs, rhs); |
| 1262 | } |
| 1263 | |
| 1264 | Value *Nucleus::createFRem(Value *lhs, Value *rhs) |
| 1265 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1266 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1267 | // TODO(b/148139679) Fix Subzero generating invalid code for FRem on vector types |
| 1268 | // createArithmetic(Ice::InstArithmetic::Frem, lhs, rhs); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 1269 | UNIMPLEMENTED("b/148139679 Nucleus::createFRem"); |
Antonio Maiorano | 5ef91b8 | 2020-01-21 15:10:22 -0500 | [diff] [blame] | 1270 | return nullptr; |
| 1271 | } |
| 1272 | |
| 1273 | RValue<Float4> operator%(RValue<Float4> lhs, RValue<Float4> rhs) |
| 1274 | { |
| 1275 | return emulated::FRem(lhs, rhs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | Value *Nucleus::createShl(Value *lhs, Value *rhs) |
| 1279 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1280 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1281 | return createArithmetic(Ice::InstArithmetic::Shl, lhs, rhs); |
| 1282 | } |
| 1283 | |
| 1284 | Value *Nucleus::createLShr(Value *lhs, Value *rhs) |
| 1285 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1286 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1287 | return createArithmetic(Ice::InstArithmetic::Lshr, lhs, rhs); |
| 1288 | } |
| 1289 | |
| 1290 | Value *Nucleus::createAShr(Value *lhs, Value *rhs) |
| 1291 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1292 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1293 | return createArithmetic(Ice::InstArithmetic::Ashr, lhs, rhs); |
| 1294 | } |
| 1295 | |
| 1296 | Value *Nucleus::createAnd(Value *lhs, Value *rhs) |
| 1297 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1298 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1299 | return createArithmetic(Ice::InstArithmetic::And, lhs, rhs); |
| 1300 | } |
| 1301 | |
| 1302 | Value *Nucleus::createOr(Value *lhs, Value *rhs) |
| 1303 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1305 | return createArithmetic(Ice::InstArithmetic::Or, lhs, rhs); |
| 1306 | } |
| 1307 | |
| 1308 | Value *Nucleus::createXor(Value *lhs, Value *rhs) |
| 1309 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1310 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1311 | return createArithmetic(Ice::InstArithmetic::Xor, lhs, rhs); |
| 1312 | } |
| 1313 | |
| 1314 | Value *Nucleus::createNeg(Value *v) |
| 1315 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1316 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1317 | return createSub(createNullValue(T(v->getType())), v); |
| 1318 | } |
| 1319 | |
| 1320 | Value *Nucleus::createFNeg(Value *v) |
| 1321 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1322 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1323 | double c[4] = { -0.0, -0.0, -0.0, -0.0 }; |
| 1324 | Value *negativeZero = Ice::isVectorType(v->getType()) ? createConstantVector(c, T(v->getType())) : V(::context->getConstantFloat(-0.0f)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1325 | |
| 1326 | return createFSub(negativeZero, v); |
| 1327 | } |
| 1328 | |
| 1329 | Value *Nucleus::createNot(Value *v) |
| 1330 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1331 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1332 | if(Ice::isScalarIntegerType(v->getType())) |
| 1333 | { |
| 1334 | return createXor(v, V(::context->getConstantInt(v->getType(), -1))); |
| 1335 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1336 | else // Vector |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1337 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1338 | int64_t c[16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1339 | return createXor(v, createConstantVector(c, T(v->getType()))); |
| 1340 | } |
| 1341 | } |
| 1342 | |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1343 | static void validateAtomicAndMemoryOrderArgs(bool atomic, std::memory_order memoryOrder) |
| 1344 | { |
| 1345 | #if defined(__i386__) || defined(__x86_64__) |
| 1346 | // We're good, atomics and strictest memory order (except seq_cst) are guaranteed. |
| 1347 | // Note that sequential memory ordering could be guaranteed by using x86's LOCK prefix. |
| 1348 | // Note also that relaxed memory order could be implemented using MOVNTPS and friends. |
| 1349 | #else |
| 1350 | if(atomic) |
| 1351 | { |
| 1352 | UNIMPLEMENTED("b/150475088 Atomic load/store not implemented for current platform"); |
| 1353 | } |
| 1354 | if(memoryOrder != std::memory_order_relaxed) |
| 1355 | { |
| 1356 | UNIMPLEMENTED("b/150475088 Memory order other than memory_order_relaxed not implemented for current platform"); |
| 1357 | } |
| 1358 | #endif |
| 1359 | |
| 1360 | // Vulkan doesn't allow sequential memory order |
| 1361 | ASSERT(memoryOrder != std::memory_order_seq_cst); |
| 1362 | } |
| 1363 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1364 | Value *Nucleus::createLoad(Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1367 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1368 | |
| 1369 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1370 | Ice::Variable *result = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1371 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1372 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1373 | { |
| 1374 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1375 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1376 | if(typeSize(type) == 4) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1377 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1378 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1379 | Int x = *Pointer<Int>(pointer); |
| 1380 | |
| 1381 | Int4 vector; |
| 1382 | vector = Insert(vector, x, 0); |
| 1383 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1384 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1385 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1386 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1387 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1388 | else if(typeSize(type) == 8) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1389 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1390 | ASSERT_MSG(!atomic, "Emulated 64-bit loads are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1391 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1392 | Int x = *Pointer<Int>(pointer); |
| 1393 | Int y = *Pointer<Int>(pointer + 4); |
| 1394 | |
| 1395 | Int4 vector; |
| 1396 | vector = Insert(vector, x, 0); |
| 1397 | vector = Insert(vector, y, 1); |
| 1398 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1399 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1400 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, result, vector.loadValue()); |
| 1401 | ::basicBlock->appendInst(bitcast); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1402 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1403 | else |
| 1404 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1405 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1406 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1407 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1408 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::LoadSubVector, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1409 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1410 | result = ::function->makeVariable(T(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1411 | auto load = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
| 1412 | load->addArg(ptr); |
| 1413 | load->addArg(::context->getConstantInt32(typeSize(type))); |
| 1414 | ::basicBlock->appendInst(load); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1415 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1416 | } |
| 1417 | else |
| 1418 | { |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1419 | result = sz::createLoad(::function, ::basicBlock, V(ptr), T(type), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1420 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1421 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 1422 | ASSERT(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1423 | return V(result); |
| 1424 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1425 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1426 | Value *Nucleus::createStore(Value *value, Value *ptr, Type *type, bool isVolatile, unsigned int align, bool atomic, std::memory_order memoryOrder) |
| 1427 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1428 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1429 | validateAtomicAndMemoryOrderArgs(atomic, memoryOrder); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1430 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1431 | #if __has_feature(memory_sanitizer) |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1432 | // Mark all (non-stack) memory writes as initialized by calling __msan_unpoison |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1433 | if(align != 0) |
| 1434 | { |
| 1435 | auto call = Ice::InstCall::create(::function, 2, nullptr, ::context->getConstantInt64(reinterpret_cast<intptr_t>(__msan_unpoison)), false); |
| 1436 | call->addArg(ptr); |
| 1437 | call->addArg(::context->getConstantInt64(typeSize(type))); |
| 1438 | ::basicBlock->appendInst(call); |
| 1439 | } |
| 1440 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 1441 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1442 | int valueType = (int)reinterpret_cast<intptr_t>(type); |
| 1443 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1444 | if((valueType & EmulatedBits) && (align != 0)) // Narrow vector not stored on stack. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1445 | { |
| 1446 | if(emulateIntrinsics) |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1447 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1448 | if(typeSize(type) == 4) |
| 1449 | { |
| 1450 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1451 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1452 | ::basicBlock->appendInst(bitcast); |
| 1453 | |
| 1454 | RValue<Int4> v(V(vector)); |
| 1455 | |
| 1456 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1457 | Int x = Extract(v, 0); |
| 1458 | *Pointer<Int>(pointer) = x; |
| 1459 | } |
| 1460 | else if(typeSize(type) == 8) |
| 1461 | { |
Antonio Maiorano | 2e6cd9c | 2020-02-28 15:48:22 -0500 | [diff] [blame] | 1462 | ASSERT_MSG(!atomic, "Emulated 64-bit stores are not atomic"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1463 | Ice::Variable *vector = ::function->makeVariable(Ice::IceType_v4i32); |
| 1464 | auto bitcast = Ice::InstCast::create(::function, Ice::InstCast::Bitcast, vector, value); |
| 1465 | ::basicBlock->appendInst(bitcast); |
| 1466 | |
| 1467 | RValue<Int4> v(V(vector)); |
| 1468 | |
| 1469 | auto pointer = RValue<Pointer<Byte>>(ptr); |
| 1470 | Int x = Extract(v, 0); |
| 1471 | *Pointer<Int>(pointer) = x; |
| 1472 | Int y = Extract(v, 1); |
| 1473 | *Pointer<Int>(pointer + 4) = y; |
| 1474 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1475 | else |
| 1476 | UNREACHABLE("typeSize(type): %d", int(typeSize(type))); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1477 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1478 | else |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1479 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1480 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::StoreSubVector, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1481 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1482 | auto store = Ice::InstIntrinsicCall::create(::function, 3, nullptr, target, intrinsic); |
| 1483 | store->addArg(value); |
| 1484 | store->addArg(ptr); |
| 1485 | store->addArg(::context->getConstantInt32(typeSize(type))); |
| 1486 | ::basicBlock->appendInst(store); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1487 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1488 | } |
| 1489 | else |
| 1490 | { |
| 1491 | ASSERT(value->getType() == T(type)); |
Antonio Maiorano | 9c0617c | 2019-11-29 10:43:16 -0500 | [diff] [blame] | 1492 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1493 | auto store = Ice::InstStore::create(::function, V(value), V(ptr), align); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1494 | ::basicBlock->appendInst(store); |
| 1495 | } |
| 1496 | |
| 1497 | return value; |
| 1498 | } |
| 1499 | |
| 1500 | Value *Nucleus::createGEP(Value *ptr, Type *type, Value *index, bool unsignedIndex) |
| 1501 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1502 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1503 | ASSERT(index->getType() == Ice::IceType_i32); |
| 1504 | |
| 1505 | if(auto *constant = llvm::dyn_cast<Ice::ConstantInteger32>(index)) |
| 1506 | { |
| 1507 | int32_t offset = constant->getValue() * (int)typeSize(type); |
| 1508 | |
| 1509 | if(offset == 0) |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1510 | { |
Ben Clayton | b7eb3a8 | 2019-11-19 00:43:50 +0000 | [diff] [blame] | 1511 | return ptr; |
| 1512 | } |
| 1513 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1514 | return createAdd(ptr, createConstantInt(offset)); |
| 1515 | } |
Nicolas Capens | bd65da9 | 2017-01-05 16:31:06 -0500 | [diff] [blame] | 1516 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1517 | if(!Ice::isByteSizedType(T(type))) |
| 1518 | { |
| 1519 | index = createMul(index, createConstantInt((int)typeSize(type))); |
| 1520 | } |
| 1521 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1522 | if(sizeof(void *) == 8) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1523 | { |
| 1524 | if(unsignedIndex) |
| 1525 | { |
| 1526 | index = createZExt(index, T(Ice::IceType_i64)); |
| 1527 | } |
| 1528 | else |
| 1529 | { |
| 1530 | index = createSExt(index, T(Ice::IceType_i64)); |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | return createAdd(ptr, index); |
| 1535 | } |
| 1536 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1537 | static Value *createAtomicRMW(Ice::Intrinsics::AtomicRMWOperation rmwOp, Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1538 | { |
| 1539 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1540 | |
| 1541 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicRMW, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1542 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1543 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1544 | auto op = ::context->getConstantInt32(rmwOp); |
| 1545 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 1546 | inst->addArg(op); |
| 1547 | inst->addArg(ptr); |
| 1548 | inst->addArg(value); |
| 1549 | inst->addArg(order); |
| 1550 | ::basicBlock->appendInst(inst); |
| 1551 | |
| 1552 | return V(result); |
| 1553 | } |
| 1554 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1555 | Value *Nucleus::createAtomicAdd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1556 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1557 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1558 | return createAtomicRMW(Ice::Intrinsics::AtomicAdd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1559 | } |
| 1560 | |
| 1561 | Value *Nucleus::createAtomicSub(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1562 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1563 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1564 | return createAtomicRMW(Ice::Intrinsics::AtomicSub, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | Value *Nucleus::createAtomicAnd(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1568 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1569 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1570 | return createAtomicRMW(Ice::Intrinsics::AtomicAnd, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | Value *Nucleus::createAtomicOr(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1574 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1575 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1576 | return createAtomicRMW(Ice::Intrinsics::AtomicOr, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | Value *Nucleus::createAtomicXor(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1580 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1581 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1582 | return createAtomicRMW(Ice::Intrinsics::AtomicXor, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | Value *Nucleus::createAtomicExchange(Value *ptr, Value *value, std::memory_order memoryOrder) |
| 1586 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1587 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1588 | return createAtomicRMW(Ice::Intrinsics::AtomicExchange, ptr, value, memoryOrder); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | Value *Nucleus::createAtomicCompareExchange(Value *ptr, Value *value, Value *compare, std::memory_order memoryOrderEqual, std::memory_order memoryOrderUnequal) |
| 1592 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1593 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 1594 | Ice::Variable *result = ::function->makeVariable(value->getType()); |
| 1595 | |
| 1596 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicCmpxchg, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_T }; |
| 1597 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 1598 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, result, target, intrinsic); |
| 1599 | auto orderEq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderEqual)); |
| 1600 | auto orderNeq = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrderUnequal)); |
| 1601 | inst->addArg(ptr); |
| 1602 | inst->addArg(compare); |
| 1603 | inst->addArg(value); |
| 1604 | inst->addArg(orderEq); |
| 1605 | inst->addArg(orderNeq); |
| 1606 | ::basicBlock->appendInst(inst); |
| 1607 | |
| 1608 | return V(result); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | static Value *createCast(Ice::InstCast::OpKind op, Value *v, Type *destType) |
| 1612 | { |
| 1613 | if(v->getType() == T(destType)) |
| 1614 | { |
| 1615 | return v; |
| 1616 | } |
| 1617 | |
| 1618 | Ice::Variable *result = ::function->makeVariable(T(destType)); |
| 1619 | Ice::InstCast *cast = Ice::InstCast::create(::function, op, result, v); |
| 1620 | ::basicBlock->appendInst(cast); |
| 1621 | |
| 1622 | return V(result); |
| 1623 | } |
| 1624 | |
| 1625 | Value *Nucleus::createTrunc(Value *v, Type *destType) |
| 1626 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1627 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1628 | return createCast(Ice::InstCast::Trunc, v, destType); |
| 1629 | } |
| 1630 | |
| 1631 | Value *Nucleus::createZExt(Value *v, Type *destType) |
| 1632 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1633 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1634 | return createCast(Ice::InstCast::Zext, v, destType); |
| 1635 | } |
| 1636 | |
| 1637 | Value *Nucleus::createSExt(Value *v, Type *destType) |
| 1638 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1639 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1640 | return createCast(Ice::InstCast::Sext, v, destType); |
| 1641 | } |
| 1642 | |
| 1643 | Value *Nucleus::createFPToUI(Value *v, Type *destType) |
| 1644 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1645 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1646 | return createCast(Ice::InstCast::Fptoui, v, destType); |
| 1647 | } |
| 1648 | |
| 1649 | Value *Nucleus::createFPToSI(Value *v, Type *destType) |
| 1650 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1651 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1652 | return createCast(Ice::InstCast::Fptosi, v, destType); |
| 1653 | } |
| 1654 | |
| 1655 | Value *Nucleus::createSIToFP(Value *v, Type *destType) |
| 1656 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1657 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1658 | return createCast(Ice::InstCast::Sitofp, v, destType); |
| 1659 | } |
| 1660 | |
| 1661 | Value *Nucleus::createFPTrunc(Value *v, Type *destType) |
| 1662 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1663 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1664 | return createCast(Ice::InstCast::Fptrunc, v, destType); |
| 1665 | } |
| 1666 | |
| 1667 | Value *Nucleus::createFPExt(Value *v, Type *destType) |
| 1668 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1669 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1670 | return createCast(Ice::InstCast::Fpext, v, destType); |
| 1671 | } |
| 1672 | |
| 1673 | Value *Nucleus::createBitCast(Value *v, Type *destType) |
| 1674 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1675 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1676 | // Bitcasts must be between types of the same logical size. But with emulated narrow vectors we need |
| 1677 | // support for casting between scalars and wide vectors. For platforms where this is not supported, |
| 1678 | // emulate them by writing to the stack and reading back as the destination type. |
| 1679 | if(emulateMismatchedBitCast) |
| 1680 | { |
| 1681 | if(!Ice::isVectorType(v->getType()) && Ice::isVectorType(T(destType))) |
| 1682 | { |
| 1683 | Value *address = allocateStackVariable(destType); |
| 1684 | createStore(v, address, T(v->getType())); |
| 1685 | return createLoad(address, destType); |
| 1686 | } |
| 1687 | else if(Ice::isVectorType(v->getType()) && !Ice::isVectorType(T(destType))) |
| 1688 | { |
| 1689 | Value *address = allocateStackVariable(T(v->getType())); |
| 1690 | createStore(v, address, T(v->getType())); |
| 1691 | return createLoad(address, destType); |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | return createCast(Ice::InstCast::Bitcast, v, destType); |
| 1696 | } |
| 1697 | |
| 1698 | static Value *createIntCompare(Ice::InstIcmp::ICond condition, Value *lhs, Value *rhs) |
| 1699 | { |
| 1700 | ASSERT(lhs->getType() == rhs->getType()); |
| 1701 | |
| 1702 | auto result = ::function->makeVariable(Ice::isScalarIntegerType(lhs->getType()) ? Ice::IceType_i1 : lhs->getType()); |
| 1703 | auto cmp = Ice::InstIcmp::create(::function, condition, result, lhs, rhs); |
| 1704 | ::basicBlock->appendInst(cmp); |
| 1705 | |
| 1706 | return V(result); |
| 1707 | } |
| 1708 | |
| 1709 | Value *Nucleus::createPtrEQ(Value *lhs, Value *rhs) |
| 1710 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1711 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1712 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1713 | } |
| 1714 | |
| 1715 | Value *Nucleus::createICmpEQ(Value *lhs, Value *rhs) |
| 1716 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1717 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1718 | return createIntCompare(Ice::InstIcmp::Eq, lhs, rhs); |
| 1719 | } |
| 1720 | |
| 1721 | Value *Nucleus::createICmpNE(Value *lhs, Value *rhs) |
| 1722 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1723 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1724 | return createIntCompare(Ice::InstIcmp::Ne, lhs, rhs); |
| 1725 | } |
| 1726 | |
| 1727 | Value *Nucleus::createICmpUGT(Value *lhs, Value *rhs) |
| 1728 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1729 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1730 | return createIntCompare(Ice::InstIcmp::Ugt, lhs, rhs); |
| 1731 | } |
| 1732 | |
| 1733 | Value *Nucleus::createICmpUGE(Value *lhs, Value *rhs) |
| 1734 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1735 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1736 | return createIntCompare(Ice::InstIcmp::Uge, lhs, rhs); |
| 1737 | } |
| 1738 | |
| 1739 | Value *Nucleus::createICmpULT(Value *lhs, Value *rhs) |
| 1740 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1741 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1742 | return createIntCompare(Ice::InstIcmp::Ult, lhs, rhs); |
| 1743 | } |
| 1744 | |
| 1745 | Value *Nucleus::createICmpULE(Value *lhs, Value *rhs) |
| 1746 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1747 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1748 | return createIntCompare(Ice::InstIcmp::Ule, lhs, rhs); |
| 1749 | } |
| 1750 | |
| 1751 | Value *Nucleus::createICmpSGT(Value *lhs, Value *rhs) |
| 1752 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1753 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1754 | return createIntCompare(Ice::InstIcmp::Sgt, lhs, rhs); |
| 1755 | } |
| 1756 | |
| 1757 | Value *Nucleus::createICmpSGE(Value *lhs, Value *rhs) |
| 1758 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1759 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1760 | return createIntCompare(Ice::InstIcmp::Sge, lhs, rhs); |
| 1761 | } |
| 1762 | |
| 1763 | Value *Nucleus::createICmpSLT(Value *lhs, Value *rhs) |
| 1764 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1765 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1766 | return createIntCompare(Ice::InstIcmp::Slt, lhs, rhs); |
| 1767 | } |
| 1768 | |
| 1769 | Value *Nucleus::createICmpSLE(Value *lhs, Value *rhs) |
| 1770 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1771 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1772 | return createIntCompare(Ice::InstIcmp::Sle, lhs, rhs); |
| 1773 | } |
| 1774 | |
| 1775 | static Value *createFloatCompare(Ice::InstFcmp::FCond condition, Value *lhs, Value *rhs) |
| 1776 | { |
| 1777 | ASSERT(lhs->getType() == rhs->getType()); |
| 1778 | ASSERT(Ice::isScalarFloatingType(lhs->getType()) || lhs->getType() == Ice::IceType_v4f32); |
| 1779 | |
| 1780 | auto result = ::function->makeVariable(Ice::isScalarFloatingType(lhs->getType()) ? Ice::IceType_i1 : Ice::IceType_v4i32); |
| 1781 | auto cmp = Ice::InstFcmp::create(::function, condition, result, lhs, rhs); |
| 1782 | ::basicBlock->appendInst(cmp); |
| 1783 | |
| 1784 | return V(result); |
| 1785 | } |
| 1786 | |
| 1787 | Value *Nucleus::createFCmpOEQ(Value *lhs, Value *rhs) |
| 1788 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1789 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1790 | return createFloatCompare(Ice::InstFcmp::Oeq, lhs, rhs); |
| 1791 | } |
| 1792 | |
| 1793 | Value *Nucleus::createFCmpOGT(Value *lhs, Value *rhs) |
| 1794 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1795 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1796 | return createFloatCompare(Ice::InstFcmp::Ogt, lhs, rhs); |
| 1797 | } |
| 1798 | |
| 1799 | Value *Nucleus::createFCmpOGE(Value *lhs, Value *rhs) |
| 1800 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1801 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1802 | return createFloatCompare(Ice::InstFcmp::Oge, lhs, rhs); |
| 1803 | } |
| 1804 | |
| 1805 | Value *Nucleus::createFCmpOLT(Value *lhs, Value *rhs) |
| 1806 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1807 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1808 | return createFloatCompare(Ice::InstFcmp::Olt, lhs, rhs); |
| 1809 | } |
| 1810 | |
| 1811 | Value *Nucleus::createFCmpOLE(Value *lhs, Value *rhs) |
| 1812 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1813 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1814 | return createFloatCompare(Ice::InstFcmp::Ole, lhs, rhs); |
| 1815 | } |
| 1816 | |
| 1817 | Value *Nucleus::createFCmpONE(Value *lhs, Value *rhs) |
| 1818 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1819 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1820 | return createFloatCompare(Ice::InstFcmp::One, lhs, rhs); |
| 1821 | } |
| 1822 | |
| 1823 | Value *Nucleus::createFCmpORD(Value *lhs, Value *rhs) |
| 1824 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1825 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1826 | return createFloatCompare(Ice::InstFcmp::Ord, lhs, rhs); |
| 1827 | } |
| 1828 | |
| 1829 | Value *Nucleus::createFCmpUNO(Value *lhs, Value *rhs) |
| 1830 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1831 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1832 | return createFloatCompare(Ice::InstFcmp::Uno, lhs, rhs); |
| 1833 | } |
| 1834 | |
| 1835 | Value *Nucleus::createFCmpUEQ(Value *lhs, Value *rhs) |
| 1836 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1837 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1838 | return createFloatCompare(Ice::InstFcmp::Ueq, lhs, rhs); |
| 1839 | } |
| 1840 | |
| 1841 | Value *Nucleus::createFCmpUGT(Value *lhs, Value *rhs) |
| 1842 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1843 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1844 | return createFloatCompare(Ice::InstFcmp::Ugt, lhs, rhs); |
| 1845 | } |
| 1846 | |
| 1847 | Value *Nucleus::createFCmpUGE(Value *lhs, Value *rhs) |
| 1848 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1849 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1850 | return createFloatCompare(Ice::InstFcmp::Uge, lhs, rhs); |
| 1851 | } |
| 1852 | |
| 1853 | Value *Nucleus::createFCmpULT(Value *lhs, Value *rhs) |
| 1854 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1855 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1856 | return createFloatCompare(Ice::InstFcmp::Ult, lhs, rhs); |
| 1857 | } |
| 1858 | |
| 1859 | Value *Nucleus::createFCmpULE(Value *lhs, Value *rhs) |
| 1860 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1861 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1862 | return createFloatCompare(Ice::InstFcmp::Ule, lhs, rhs); |
| 1863 | } |
| 1864 | |
| 1865 | Value *Nucleus::createFCmpUNE(Value *lhs, Value *rhs) |
| 1866 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1867 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1868 | return createFloatCompare(Ice::InstFcmp::Une, lhs, rhs); |
| 1869 | } |
| 1870 | |
| 1871 | Value *Nucleus::createExtractElement(Value *vector, Type *type, int index) |
| 1872 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1873 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1874 | auto result = ::function->makeVariable(T(type)); |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1875 | auto extract = Ice::InstExtractElement::create(::function, result, V(vector), ::context->getConstantInt32(index)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1876 | ::basicBlock->appendInst(extract); |
| 1877 | |
| 1878 | return V(result); |
| 1879 | } |
| 1880 | |
| 1881 | Value *Nucleus::createInsertElement(Value *vector, Value *element, int index) |
| 1882 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1883 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1884 | auto result = ::function->makeVariable(vector->getType()); |
| 1885 | auto insert = Ice::InstInsertElement::create(::function, result, vector, element, ::context->getConstantInt32(index)); |
| 1886 | ::basicBlock->appendInst(insert); |
| 1887 | |
| 1888 | return V(result); |
| 1889 | } |
| 1890 | |
| 1891 | Value *Nucleus::createShuffleVector(Value *V1, Value *V2, const int *select) |
| 1892 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1893 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1894 | ASSERT(V1->getType() == V2->getType()); |
| 1895 | |
| 1896 | int size = Ice::typeNumElements(V1->getType()); |
| 1897 | auto result = ::function->makeVariable(V1->getType()); |
| 1898 | auto shuffle = Ice::InstShuffleVector::create(::function, result, V1, V2); |
| 1899 | |
| 1900 | for(int i = 0; i < size; i++) |
| 1901 | { |
| 1902 | shuffle->addIndex(llvm::cast<Ice::ConstantInteger32>(::context->getConstantInt32(select[i]))); |
| 1903 | } |
| 1904 | |
| 1905 | ::basicBlock->appendInst(shuffle); |
| 1906 | |
| 1907 | return V(result); |
| 1908 | } |
| 1909 | |
| 1910 | Value *Nucleus::createSelect(Value *C, Value *ifTrue, Value *ifFalse) |
| 1911 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1912 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1913 | ASSERT(ifTrue->getType() == ifFalse->getType()); |
| 1914 | |
| 1915 | auto result = ::function->makeVariable(ifTrue->getType()); |
| 1916 | auto *select = Ice::InstSelect::create(::function, result, C, ifTrue, ifFalse); |
| 1917 | ::basicBlock->appendInst(select); |
| 1918 | |
| 1919 | return V(result); |
| 1920 | } |
| 1921 | |
| 1922 | SwitchCases *Nucleus::createSwitch(Value *control, BasicBlock *defaultBranch, unsigned numCases) |
| 1923 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1924 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1925 | auto switchInst = Ice::InstSwitch::create(::function, numCases, control, defaultBranch); |
| 1926 | ::basicBlock->appendInst(switchInst); |
| 1927 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 1928 | return reinterpret_cast<SwitchCases *>(switchInst); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | void Nucleus::addSwitchCase(SwitchCases *switchCases, int label, BasicBlock *branch) |
| 1932 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1933 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1934 | switchCases->addBranch(label, label, branch); |
| 1935 | } |
| 1936 | |
| 1937 | void Nucleus::createUnreachable() |
| 1938 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1939 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1940 | Ice::InstUnreachable *unreachable = Ice::InstUnreachable::create(::function); |
| 1941 | ::basicBlock->appendInst(unreachable); |
| 1942 | } |
| 1943 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1944 | Type *Nucleus::getType(Value *value) |
| 1945 | { |
| 1946 | return T(V(value)->getType()); |
| 1947 | } |
| 1948 | |
| 1949 | Type *Nucleus::getContainedType(Type *vectorType) |
| 1950 | { |
| 1951 | Ice::Type vecTy = T(vectorType); |
| 1952 | switch(vecTy) |
| 1953 | { |
| 1954 | case Ice::IceType_v4i1: return T(Ice::IceType_i1); |
| 1955 | case Ice::IceType_v8i1: return T(Ice::IceType_i1); |
| 1956 | case Ice::IceType_v16i1: return T(Ice::IceType_i1); |
| 1957 | case Ice::IceType_v16i8: return T(Ice::IceType_i8); |
| 1958 | case Ice::IceType_v8i16: return T(Ice::IceType_i16); |
| 1959 | case Ice::IceType_v4i32: return T(Ice::IceType_i32); |
| 1960 | case Ice::IceType_v4f32: return T(Ice::IceType_f32); |
| 1961 | default: |
| 1962 | ASSERT_MSG(false, "getContainedType: input type is not a vector type"); |
| 1963 | return {}; |
| 1964 | } |
| 1965 | } |
| 1966 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1967 | Type *Nucleus::getPointerType(Type *ElementType) |
| 1968 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 1969 | return T(sz::getPointerType(T(ElementType))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1970 | } |
| 1971 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 1972 | static constexpr Ice::Type getNaturalIntType() |
| 1973 | { |
| 1974 | constexpr size_t intSize = sizeof(int); |
| 1975 | static_assert(intSize == 4 || intSize == 8, ""); |
| 1976 | return intSize == 4 ? Ice::IceType_i32 : Ice::IceType_i64; |
| 1977 | } |
| 1978 | |
| 1979 | Type *Nucleus::getPrintfStorageType(Type *valueType) |
| 1980 | { |
| 1981 | Ice::Type valueTy = T(valueType); |
| 1982 | switch(valueTy) |
| 1983 | { |
| 1984 | case Ice::IceType_i32: |
| 1985 | return T(getNaturalIntType()); |
| 1986 | |
| 1987 | case Ice::IceType_f32: |
| 1988 | return T(Ice::IceType_f64); |
| 1989 | |
| 1990 | default: |
| 1991 | UNIMPLEMENTED_NO_BUG("getPrintfStorageType: add more cases as needed"); |
| 1992 | return {}; |
| 1993 | } |
| 1994 | } |
| 1995 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1996 | Value *Nucleus::createNullValue(Type *Ty) |
| 1997 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 1998 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 1999 | if(Ice::isVectorType(T(Ty))) |
| 2000 | { |
| 2001 | ASSERT(Ice::typeNumElements(T(Ty)) <= 16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2002 | int64_t c[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2003 | return createConstantVector(c, Ty); |
| 2004 | } |
| 2005 | else |
| 2006 | { |
| 2007 | return V(::context->getConstantZero(T(Ty))); |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | Value *Nucleus::createConstantLong(int64_t i) |
| 2012 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2013 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2014 | return V(::context->getConstantInt64(i)); |
| 2015 | } |
| 2016 | |
| 2017 | Value *Nucleus::createConstantInt(int i) |
| 2018 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2019 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2020 | return V(::context->getConstantInt32(i)); |
| 2021 | } |
| 2022 | |
| 2023 | Value *Nucleus::createConstantInt(unsigned int i) |
| 2024 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2025 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2026 | return V(::context->getConstantInt32(i)); |
| 2027 | } |
| 2028 | |
| 2029 | Value *Nucleus::createConstantBool(bool b) |
| 2030 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2031 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2032 | return V(::context->getConstantInt1(b)); |
| 2033 | } |
| 2034 | |
| 2035 | Value *Nucleus::createConstantByte(signed char i) |
| 2036 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2037 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2038 | return V(::context->getConstantInt8(i)); |
| 2039 | } |
| 2040 | |
| 2041 | Value *Nucleus::createConstantByte(unsigned char i) |
| 2042 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2043 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2044 | return V(::context->getConstantInt8(i)); |
| 2045 | } |
| 2046 | |
| 2047 | Value *Nucleus::createConstantShort(short i) |
| 2048 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2049 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2050 | return V(::context->getConstantInt16(i)); |
| 2051 | } |
| 2052 | |
| 2053 | Value *Nucleus::createConstantShort(unsigned short i) |
| 2054 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2055 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2056 | return V(::context->getConstantInt16(i)); |
| 2057 | } |
| 2058 | |
| 2059 | Value *Nucleus::createConstantFloat(float x) |
| 2060 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2061 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2062 | return V(::context->getConstantFloat(x)); |
| 2063 | } |
| 2064 | |
| 2065 | Value *Nucleus::createNullPointer(Type *Ty) |
| 2066 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2067 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2068 | return createNullValue(T(sizeof(void *) == 8 ? Ice::IceType_i64 : Ice::IceType_i32)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2069 | } |
| 2070 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2071 | static Ice::Constant *IceConstantData(void const *data, size_t size, size_t alignment = 1) |
| 2072 | { |
| 2073 | return sz::getConstantPointer(::context, ::routine->addConstantData(data, size, alignment)); |
| 2074 | } |
| 2075 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2076 | Value *Nucleus::createConstantVector(const int64_t *constants, Type *type) |
| 2077 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2078 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2079 | const int vectorSize = 16; |
| 2080 | ASSERT(Ice::typeWidthInBytes(T(type)) == vectorSize); |
| 2081 | const int alignment = vectorSize; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2082 | |
| 2083 | const int64_t *i = constants; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2084 | const double *f = reinterpret_cast<const double *>(constants); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2085 | |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2086 | // TODO(b/148082873): Fix global variable constants when generating multiple functions |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2087 | Ice::Constant *ptr = nullptr; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2088 | |
| 2089 | switch((int)reinterpret_cast<intptr_t>(type)) |
| 2090 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2091 | case Ice::IceType_v4i32: |
| 2092 | case Ice::IceType_v4i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2093 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2094 | const int initializer[4] = { (int)i[0], (int)i[1], (int)i[2], (int)i[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2095 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2096 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2097 | } |
| 2098 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2099 | case Ice::IceType_v4f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2100 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2101 | const float initializer[4] = { (float)f[0], (float)f[1], (float)f[2], (float)f[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2102 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2103 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2104 | } |
| 2105 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2106 | case Ice::IceType_v8i16: |
| 2107 | case Ice::IceType_v8i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2108 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2109 | const short initializer[8] = { (short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[4], (short)i[5], (short)i[6], (short)i[7] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2110 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2111 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2112 | } |
| 2113 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2114 | case Ice::IceType_v16i8: |
| 2115 | case Ice::IceType_v16i1: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2116 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2117 | const char initializer[16] = { (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[8], (char)i[9], (char)i[10], (char)i[11], (char)i[12], (char)i[13], (char)i[14], (char)i[15] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2118 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2119 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2120 | } |
| 2121 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2122 | case Type_v2i32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2123 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2124 | const int initializer[4] = { (int)i[0], (int)i[1], (int)i[0], (int)i[1] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2125 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2126 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2127 | } |
| 2128 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2129 | case Type_v2f32: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2130 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2131 | const float initializer[4] = { (float)f[0], (float)f[1], (float)f[0], (float)f[1] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2132 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2133 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2134 | } |
| 2135 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2136 | case Type_v4i16: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2137 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2138 | const short initializer[8] = { (short)i[0], (short)i[1], (short)i[2], (short)i[3], (short)i[0], (short)i[1], (short)i[2], (short)i[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2139 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2140 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2141 | } |
| 2142 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2143 | case Type_v8i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2144 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2145 | const char initializer[16] = { (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[4], (char)i[5], (char)i[6], (char)i[7] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2146 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2147 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2148 | } |
| 2149 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2150 | case Type_v4i8: |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2151 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2152 | const char initializer[16] = { (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3], (char)i[0], (char)i[1], (char)i[2], (char)i[3] }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2153 | static_assert(sizeof(initializer) == vectorSize, "!"); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2154 | ptr = IceConstantData(initializer, vectorSize, alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2155 | } |
| 2156 | break; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2157 | default: |
| 2158 | UNREACHABLE("Unknown constant vector type: %d", (int)reinterpret_cast<intptr_t>(type)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2159 | } |
| 2160 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2161 | ASSERT(ptr); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2162 | |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 2163 | Ice::Variable *result = sz::createLoad(::function, ::basicBlock, ptr, T(type), alignment); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2164 | return V(result); |
| 2165 | } |
| 2166 | |
| 2167 | Value *Nucleus::createConstantVector(const double *constants, Type *type) |
| 2168 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2169 | return createConstantVector((const int64_t *)constants, type); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2170 | } |
| 2171 | |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2172 | Value *Nucleus::createConstantString(const char *v) |
| 2173 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2174 | // NOTE: Do not call RR_DEBUG_INFO_UPDATE_LOC() here to avoid recursion when called from rr::Printv |
Antonio Maiorano | 62427e0 | 2020-02-13 09:18:05 -0500 | [diff] [blame] | 2175 | return V(IceConstantData(v, strlen(v) + 1)); |
| 2176 | } |
| 2177 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2178 | Type *Void::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2179 | { |
| 2180 | return T(Ice::IceType_void); |
| 2181 | } |
| 2182 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2183 | Type *Bool::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2184 | { |
| 2185 | return T(Ice::IceType_i1); |
| 2186 | } |
| 2187 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2188 | Type *Byte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2189 | { |
| 2190 | return T(Ice::IceType_i8); |
| 2191 | } |
| 2192 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2193 | Type *SByte::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2194 | { |
| 2195 | return T(Ice::IceType_i8); |
| 2196 | } |
| 2197 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2198 | Type *Short::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2199 | { |
| 2200 | return T(Ice::IceType_i16); |
| 2201 | } |
| 2202 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2203 | Type *UShort::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2204 | { |
| 2205 | return T(Ice::IceType_i16); |
| 2206 | } |
| 2207 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2208 | Type *Byte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2209 | { |
| 2210 | return T(Type_v4i8); |
| 2211 | } |
| 2212 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2213 | Type *SByte4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2214 | { |
| 2215 | return T(Type_v4i8); |
| 2216 | } |
| 2217 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2218 | namespace { |
| 2219 | RValue<Byte> SaturateUnsigned(RValue<Short> x) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2220 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2221 | return Byte(IfThenElse(Int(x) > 0xFF, Int(0xFF), IfThenElse(Int(x) < 0, Int(0), Int(x)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2222 | } |
| 2223 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2224 | RValue<Byte> Extract(RValue<Byte8> val, int i) |
| 2225 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2226 | return RValue<Byte>(Nucleus::createExtractElement(val.value(), Byte::type(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
| 2229 | RValue<Byte8> Insert(RValue<Byte8> val, RValue<Byte> element, int i) |
| 2230 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2231 | return RValue<Byte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2232 | } |
| 2233 | } // namespace |
| 2234 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2235 | RValue<Byte8> AddSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2236 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2237 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2238 | if(emulateIntrinsics) |
| 2239 | { |
| 2240 | Byte8 result; |
| 2241 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2242 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2243 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2244 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2245 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2246 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2247 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2248 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
| 2249 | |
| 2250 | return result; |
| 2251 | } |
| 2252 | else |
| 2253 | { |
| 2254 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2255 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2256 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2257 | auto paddusb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2258 | paddusb->addArg(x.value()); |
| 2259 | paddusb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2260 | ::basicBlock->appendInst(paddusb); |
| 2261 | |
| 2262 | return RValue<Byte8>(V(result)); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | RValue<Byte8> SubSat(RValue<Byte8> x, RValue<Byte8> y) |
| 2267 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2268 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2269 | if(emulateIntrinsics) |
| 2270 | { |
| 2271 | Byte8 result; |
| 2272 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2273 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2274 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2275 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2276 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2277 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2278 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2279 | result = Insert(result, SaturateUnsigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
| 2280 | |
| 2281 | return result; |
| 2282 | } |
| 2283 | else |
| 2284 | { |
| 2285 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2286 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2287 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2288 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2289 | psubusw->addArg(x.value()); |
| 2290 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2291 | ::basicBlock->appendInst(psubusw); |
| 2292 | |
| 2293 | return RValue<Byte8>(V(result)); |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | RValue<SByte> Extract(RValue<SByte8> val, int i) |
| 2298 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2299 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2300 | return RValue<SByte>(Nucleus::createExtractElement(val.value(), SByte::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | RValue<SByte8> Insert(RValue<SByte8> val, RValue<SByte> element, int i) |
| 2304 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2305 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2306 | return RValue<SByte8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2310 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2311 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2312 | if(emulateIntrinsics) |
| 2313 | { |
| 2314 | SByte8 result; |
| 2315 | result = Insert(result, Extract(lhs, 0) >> SByte(rhs), 0); |
| 2316 | result = Insert(result, Extract(lhs, 1) >> SByte(rhs), 1); |
| 2317 | result = Insert(result, Extract(lhs, 2) >> SByte(rhs), 2); |
| 2318 | result = Insert(result, Extract(lhs, 3) >> SByte(rhs), 3); |
| 2319 | result = Insert(result, Extract(lhs, 4) >> SByte(rhs), 4); |
| 2320 | result = Insert(result, Extract(lhs, 5) >> SByte(rhs), 5); |
| 2321 | result = Insert(result, Extract(lhs, 6) >> SByte(rhs), 6); |
| 2322 | result = Insert(result, Extract(lhs, 7) >> SByte(rhs), 7); |
| 2323 | |
| 2324 | return result; |
| 2325 | } |
| 2326 | else |
| 2327 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2328 | #if defined(__i386__) || defined(__x86_64__) |
| 2329 | // SSE2 doesn't support byte vector shifts, so shift as shorts and recombine. |
| 2330 | RValue<Short4> hi = (As<Short4>(lhs) >> rhs) & Short4(0xFF00u); |
| 2331 | RValue<Short4> lo = As<Short4>(As<UShort4>((As<Short4>(lhs) << 8) >> rhs) >> 8); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2332 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2333 | return As<SByte8>(hi | lo); |
| 2334 | #else |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2335 | return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2336 | #endif |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2337 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2338 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2339 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2340 | RValue<Int> SignMask(RValue<Byte8> x) |
| 2341 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2342 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2343 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2344 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2345 | Byte8 xx = As<Byte8>(As<SByte8>(x) >> 7) & Byte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2346 | return Int(Extract(xx, 0)) | Int(Extract(xx, 1)) | Int(Extract(xx, 2)) | Int(Extract(xx, 3)) | Int(Extract(xx, 4)) | Int(Extract(xx, 5)) | Int(Extract(xx, 6)) | Int(Extract(xx, 7)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2347 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2348 | else |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2349 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2350 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2351 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2352 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2353 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2354 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2355 | ::basicBlock->appendInst(movmsk); |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2356 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2357 | return RValue<Int>(V(result)) & 0xFF; |
Ben Clayton | 55bc37a | 2019-07-04 12:17:12 +0100 | [diff] [blame] | 2358 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2359 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2360 | |
| 2361 | // RValue<Byte8> CmpGT(RValue<Byte8> x, RValue<Byte8> y) |
| 2362 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2363 | // return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Ugt, x.value(), y.value())); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2364 | // } |
| 2365 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2366 | RValue<Byte8> CmpEQ(RValue<Byte8> x, RValue<Byte8> y) |
| 2367 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2368 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2369 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2370 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2371 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2372 | Type *Byte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2373 | { |
| 2374 | return T(Type_v8i8); |
| 2375 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2376 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2377 | // RValue<SByte8> operator<<(RValue<SByte8> lhs, unsigned char rhs) |
| 2378 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2379 | // return RValue<SByte8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2380 | // } |
| 2381 | |
| 2382 | // RValue<SByte8> operator>>(RValue<SByte8> lhs, unsigned char rhs) |
| 2383 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2384 | // return RValue<SByte8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2385 | // } |
| 2386 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2387 | RValue<SByte> SaturateSigned(RValue<Short> x) |
| 2388 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2389 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2390 | return SByte(IfThenElse(Int(x) > 0x7F, Int(0x7F), IfThenElse(Int(x) < -0x80, Int(0x80), Int(x)))); |
| 2391 | } |
| 2392 | |
| 2393 | RValue<SByte8> AddSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2394 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2395 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2396 | if(emulateIntrinsics) |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2397 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2398 | SByte8 result; |
| 2399 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) + Int(Extract(y, 0)))), 0); |
| 2400 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) + Int(Extract(y, 1)))), 1); |
| 2401 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) + Int(Extract(y, 2)))), 2); |
| 2402 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) + Int(Extract(y, 3)))), 3); |
| 2403 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) + Int(Extract(y, 4)))), 4); |
| 2404 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) + Int(Extract(y, 5)))), 5); |
| 2405 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) + Int(Extract(y, 6)))), 6); |
| 2406 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 7)) + Int(Extract(y, 7)))), 7); |
Nicolas Capens | 9843673 | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2407 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2408 | return result; |
| 2409 | } |
| 2410 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2411 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2412 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2413 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2414 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2415 | auto paddsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2416 | paddsb->addArg(x.value()); |
| 2417 | paddsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2418 | ::basicBlock->appendInst(paddsb); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2419 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2420 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2421 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2422 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2423 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2424 | RValue<SByte8> SubSat(RValue<SByte8> x, RValue<SByte8> y) |
| 2425 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2427 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2428 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2429 | SByte8 result; |
| 2430 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 0)) - Int(Extract(y, 0)))), 0); |
| 2431 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 1)) - Int(Extract(y, 1)))), 1); |
| 2432 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 2)) - Int(Extract(y, 2)))), 2); |
| 2433 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 3)) - Int(Extract(y, 3)))), 3); |
| 2434 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 4)) - Int(Extract(y, 4)))), 4); |
| 2435 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 5)) - Int(Extract(y, 5)))), 5); |
| 2436 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 6)) - Int(Extract(y, 6)))), 6); |
| 2437 | result = Insert(result, SaturateSigned(Short(Int(Extract(x, 7)) - Int(Extract(y, 7)))), 7); |
Nicolas Capens | c71bed2 | 2016-11-07 22:25:14 -0500 | [diff] [blame] | 2438 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2439 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2440 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2441 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2442 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2443 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2444 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2445 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2446 | auto psubsb = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2447 | psubsb->addArg(x.value()); |
| 2448 | psubsb->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2449 | ::basicBlock->appendInst(psubsb); |
Nicolas Capens | f2cb9df | 2016-10-21 17:26:13 -0400 | [diff] [blame] | 2450 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2451 | return RValue<SByte8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2452 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2453 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2454 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2455 | RValue<Int> SignMask(RValue<SByte8> x) |
| 2456 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2457 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2458 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2459 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2460 | SByte8 xx = (x >> 7) & SByte8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80); |
| 2461 | return Int(Extract(xx, 0)) | Int(Extract(xx, 1)) | Int(Extract(xx, 2)) | Int(Extract(xx, 3)) | Int(Extract(xx, 4)) | Int(Extract(xx, 5)) | Int(Extract(xx, 6)) | Int(Extract(xx, 7)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2462 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2463 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2464 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2465 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2466 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2467 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2468 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2469 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2470 | ::basicBlock->appendInst(movmsk); |
| 2471 | |
| 2472 | return RValue<Int>(V(result)) & 0xFF; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2473 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2474 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2475 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2476 | RValue<Byte8> CmpGT(RValue<SByte8> x, RValue<SByte8> y) |
| 2477 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2478 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2479 | return RValue<Byte8>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2480 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2481 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2482 | RValue<Byte8> CmpEQ(RValue<SByte8> x, RValue<SByte8> y) |
| 2483 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2484 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2485 | return RValue<Byte8>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2486 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2487 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2488 | Type *SByte8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2489 | { |
| 2490 | return T(Type_v8i8); |
| 2491 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2492 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2493 | Type *Byte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2494 | { |
| 2495 | return T(Ice::IceType_v16i8); |
| 2496 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2497 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2498 | Type *SByte16::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2499 | { |
| 2500 | return T(Ice::IceType_v16i8); |
| 2501 | } |
Nicolas Capens | 16b5f15 | 2016-10-13 13:39:01 -0400 | [diff] [blame] | 2502 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2503 | Type *Short2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2504 | { |
| 2505 | return T(Type_v2i16); |
| 2506 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2507 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2508 | Type *UShort2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2509 | { |
| 2510 | return T(Type_v2i16); |
| 2511 | } |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 2512 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2513 | Short4::Short4(RValue<Int4> cast) |
| 2514 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2515 | int select[8] = { 0, 2, 4, 6, 0, 2, 4, 6 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2516 | Value *short8 = Nucleus::createBitCast(cast.value(), Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2517 | Value *packed = Nucleus::createShuffleVector(short8, short8, select); |
| 2518 | |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2519 | Value *int2 = RValue<Int2>(Int2(As<Int4>(packed))).value(); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2520 | Value *short4 = Nucleus::createBitCast(int2, Short4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2521 | |
| 2522 | storeValue(short4); |
| 2523 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2524 | |
| 2525 | // Short4::Short4(RValue<Float> cast) |
| 2526 | // { |
| 2527 | // } |
| 2528 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2529 | Short4::Short4(RValue<Float4> cast) |
| 2530 | { |
Antonio Maiorano | a095711 | 2020-03-04 15:06:19 -0500 | [diff] [blame] | 2531 | // TODO(b/150791192): Generalize and optimize |
| 2532 | auto smin = std::numeric_limits<short>::min(); |
| 2533 | auto smax = std::numeric_limits<short>::max(); |
| 2534 | *this = Short4(Int4(Max(Min(cast, Float4(smax)), Float4(smin)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | RValue<Short4> operator<<(RValue<Short4> lhs, unsigned char rhs) |
| 2538 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2539 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2540 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2541 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2542 | Short4 result; |
| 2543 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 2544 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 2545 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 2546 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2547 | |
| 2548 | return result; |
| 2549 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2550 | else |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2551 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2552 | return RValue<Short4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2553 | } |
| 2554 | } |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2555 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2556 | RValue<Short4> operator>>(RValue<Short4> lhs, unsigned char rhs) |
| 2557 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2558 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2559 | if(emulateIntrinsics) |
| 2560 | { |
| 2561 | Short4 result; |
| 2562 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 2563 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 2564 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 2565 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 2566 | |
| 2567 | return result; |
| 2568 | } |
| 2569 | else |
| 2570 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2571 | return RValue<Short4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2572 | } |
| 2573 | } |
| 2574 | |
| 2575 | RValue<Short4> Max(RValue<Short4> x, RValue<Short4> y) |
| 2576 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2577 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2578 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2579 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2580 | ::basicBlock->appendInst(cmp); |
| 2581 | |
| 2582 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2583 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2584 | ::basicBlock->appendInst(select); |
| 2585 | |
| 2586 | return RValue<Short4>(V(result)); |
| 2587 | } |
| 2588 | |
| 2589 | RValue<Short4> Min(RValue<Short4> x, RValue<Short4> y) |
| 2590 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2591 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2592 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2593 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2594 | ::basicBlock->appendInst(cmp); |
| 2595 | |
| 2596 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2597 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2598 | ::basicBlock->appendInst(select); |
| 2599 | |
| 2600 | return RValue<Short4>(V(result)); |
| 2601 | } |
| 2602 | |
| 2603 | RValue<Short> SaturateSigned(RValue<Int> x) |
| 2604 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2605 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2606 | return Short(IfThenElse(x > 0x7FFF, Int(0x7FFF), IfThenElse(x < -0x8000, Int(0x8000), x))); |
| 2607 | } |
| 2608 | |
| 2609 | RValue<Short4> AddSat(RValue<Short4> x, RValue<Short4> y) |
| 2610 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2611 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2612 | if(emulateIntrinsics) |
| 2613 | { |
| 2614 | Short4 result; |
| 2615 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2616 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2617 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2618 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2619 | |
| 2620 | return result; |
| 2621 | } |
| 2622 | else |
| 2623 | { |
| 2624 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2625 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2626 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2627 | auto paddsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2628 | paddsw->addArg(x.value()); |
| 2629 | paddsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2630 | ::basicBlock->appendInst(paddsw); |
| 2631 | |
| 2632 | return RValue<Short4>(V(result)); |
| 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | RValue<Short4> SubSat(RValue<Short4> x, RValue<Short4> y) |
| 2637 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2638 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2639 | if(emulateIntrinsics) |
| 2640 | { |
| 2641 | Short4 result; |
| 2642 | result = Insert(result, SaturateSigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2643 | result = Insert(result, SaturateSigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2644 | result = Insert(result, SaturateSigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2645 | result = Insert(result, SaturateSigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2646 | |
| 2647 | return result; |
| 2648 | } |
| 2649 | else |
| 2650 | { |
| 2651 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2652 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2653 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2654 | auto psubsw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2655 | psubsw->addArg(x.value()); |
| 2656 | psubsw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2657 | ::basicBlock->appendInst(psubsw); |
| 2658 | |
| 2659 | return RValue<Short4>(V(result)); |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | RValue<Short4> MulHigh(RValue<Short4> x, RValue<Short4> y) |
| 2664 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2665 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2666 | if(emulateIntrinsics) |
| 2667 | { |
| 2668 | Short4 result; |
| 2669 | result = Insert(result, Short((Int(Extract(x, 0)) * Int(Extract(y, 0))) >> 16), 0); |
| 2670 | result = Insert(result, Short((Int(Extract(x, 1)) * Int(Extract(y, 1))) >> 16), 1); |
| 2671 | result = Insert(result, Short((Int(Extract(x, 2)) * Int(Extract(y, 2))) >> 16), 2); |
| 2672 | result = Insert(result, Short((Int(Extract(x, 3)) * Int(Extract(y, 3))) >> 16), 3); |
| 2673 | |
| 2674 | return result; |
| 2675 | } |
| 2676 | else |
| 2677 | { |
| 2678 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2679 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyHighSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2680 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2681 | auto pmulhw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2682 | pmulhw->addArg(x.value()); |
| 2683 | pmulhw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2684 | ::basicBlock->appendInst(pmulhw); |
| 2685 | |
| 2686 | return RValue<Short4>(V(result)); |
| 2687 | } |
| 2688 | } |
| 2689 | |
| 2690 | RValue<Int2> MulAdd(RValue<Short4> x, RValue<Short4> y) |
| 2691 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2692 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2693 | if(emulateIntrinsics) |
| 2694 | { |
| 2695 | Int2 result; |
| 2696 | result = Insert(result, Int(Extract(x, 0)) * Int(Extract(y, 0)) + Int(Extract(x, 1)) * Int(Extract(y, 1)), 0); |
| 2697 | result = Insert(result, Int(Extract(x, 2)) * Int(Extract(y, 2)) + Int(Extract(x, 3)) * Int(Extract(y, 3)), 1); |
| 2698 | |
| 2699 | return result; |
| 2700 | } |
| 2701 | else |
| 2702 | { |
| 2703 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2704 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyAddPairs, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2705 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2706 | auto pmaddwd = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2707 | pmaddwd->addArg(x.value()); |
| 2708 | pmaddwd->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2709 | ::basicBlock->appendInst(pmaddwd); |
| 2710 | |
| 2711 | return As<Int2>(V(result)); |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | RValue<SByte8> PackSigned(RValue<Short4> x, RValue<Short4> y) |
| 2716 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2717 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2718 | if(emulateIntrinsics) |
| 2719 | { |
| 2720 | SByte8 result; |
| 2721 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 2722 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 2723 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 2724 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 2725 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 2726 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 2727 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 2728 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
| 2729 | |
| 2730 | return result; |
| 2731 | } |
| 2732 | else |
| 2733 | { |
| 2734 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2735 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2736 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2737 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2738 | pack->addArg(x.value()); |
| 2739 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2740 | ::basicBlock->appendInst(pack); |
| 2741 | |
| 2742 | return As<SByte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | RValue<Byte8> PackUnsigned(RValue<Short4> x, RValue<Short4> y) |
| 2747 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2748 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2749 | if(emulateIntrinsics) |
| 2750 | { |
| 2751 | Byte8 result; |
| 2752 | result = Insert(result, SaturateUnsigned(Extract(x, 0)), 0); |
| 2753 | result = Insert(result, SaturateUnsigned(Extract(x, 1)), 1); |
| 2754 | result = Insert(result, SaturateUnsigned(Extract(x, 2)), 2); |
| 2755 | result = Insert(result, SaturateUnsigned(Extract(x, 3)), 3); |
| 2756 | result = Insert(result, SaturateUnsigned(Extract(y, 0)), 4); |
| 2757 | result = Insert(result, SaturateUnsigned(Extract(y, 1)), 5); |
| 2758 | result = Insert(result, SaturateUnsigned(Extract(y, 2)), 6); |
| 2759 | result = Insert(result, SaturateUnsigned(Extract(y, 3)), 7); |
| 2760 | |
| 2761 | return result; |
| 2762 | } |
| 2763 | else |
| 2764 | { |
| 2765 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v16i8); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2766 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2767 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2768 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2769 | pack->addArg(x.value()); |
| 2770 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2771 | ::basicBlock->appendInst(pack); |
| 2772 | |
| 2773 | return As<Byte8>(Swizzle(As<Int4>(V(result)), 0x0202)); |
| 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | RValue<Short4> CmpGT(RValue<Short4> x, RValue<Short4> y) |
| 2778 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2779 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2780 | return RValue<Short4>(createIntCompare(Ice::InstIcmp::Sgt, x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2781 | } |
| 2782 | |
| 2783 | RValue<Short4> CmpEQ(RValue<Short4> x, RValue<Short4> y) |
| 2784 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2785 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2786 | return RValue<Short4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2787 | } |
| 2788 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 2789 | Type *Short4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2790 | { |
| 2791 | return T(Type_v4i16); |
| 2792 | } |
| 2793 | |
| 2794 | UShort4::UShort4(RValue<Float4> cast, bool saturate) |
| 2795 | { |
| 2796 | if(saturate) |
| 2797 | { |
| 2798 | if(CPUID::SSE4_1) |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2799 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2800 | // x86 produces 0x80000000 on 32-bit integer overflow/underflow. |
| 2801 | // PackUnsigned takes care of 0x0000 saturation. |
| 2802 | Int4 int4(Min(cast, Float4(0xFFFF))); |
| 2803 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Chris Forbes | aa8f699 | 2019-03-01 14:18:30 -0800 | [diff] [blame] | 2804 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2805 | else if(CPUID::ARM) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2806 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2807 | // ARM saturates the 32-bit integer result on overflow/undeflow. |
| 2808 | Int4 int4(cast); |
| 2809 | *this = As<UShort4>(PackUnsigned(int4, int4)); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2810 | } |
| 2811 | else |
| 2812 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2813 | *this = Short4(Int4(Max(Min(cast, Float4(0xFFFF)), Float4(0x0000)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2814 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2815 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2816 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 2817 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2818 | *this = Short4(Int4(cast)); |
| 2819 | } |
| 2820 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 2821 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2822 | RValue<UShort> Extract(RValue<UShort4> val, int i) |
| 2823 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2824 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | RValue<UShort4> Insert(RValue<UShort4> val, RValue<UShort> element, int i) |
| 2828 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2829 | return RValue<UShort4>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | RValue<UShort4> operator<<(RValue<UShort4> lhs, unsigned char rhs) |
| 2833 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2834 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2835 | if(emulateIntrinsics) |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2836 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2837 | { |
| 2838 | UShort4 result; |
| 2839 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 2840 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 2841 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 2842 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 2843 | |
| 2844 | return result; |
| 2845 | } |
| 2846 | else |
| 2847 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2848 | return RValue<UShort4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | |
| 2852 | RValue<UShort4> operator>>(RValue<UShort4> lhs, unsigned char rhs) |
| 2853 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2854 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2855 | if(emulateIntrinsics) |
| 2856 | { |
| 2857 | UShort4 result; |
| 2858 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 2859 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 2860 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 2861 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 2862 | |
| 2863 | return result; |
| 2864 | } |
| 2865 | else |
| 2866 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2867 | return RValue<UShort4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | RValue<UShort4> Max(RValue<UShort4> x, RValue<UShort4> y) |
| 2872 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2873 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2874 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2875 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2876 | ::basicBlock->appendInst(cmp); |
| 2877 | |
| 2878 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2879 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2880 | ::basicBlock->appendInst(select); |
| 2881 | |
| 2882 | return RValue<UShort4>(V(result)); |
| 2883 | } |
| 2884 | |
| 2885 | RValue<UShort4> Min(RValue<UShort4> x, RValue<UShort4> y) |
| 2886 | { |
| 2887 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v8i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2888 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2889 | ::basicBlock->appendInst(cmp); |
| 2890 | |
| 2891 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2892 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2893 | ::basicBlock->appendInst(select); |
| 2894 | |
| 2895 | return RValue<UShort4>(V(result)); |
| 2896 | } |
| 2897 | |
| 2898 | RValue<UShort> SaturateUnsigned(RValue<Int> x) |
| 2899 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2900 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2901 | return UShort(IfThenElse(x > 0xFFFF, Int(0xFFFF), IfThenElse(x < 0, Int(0), x))); |
| 2902 | } |
| 2903 | |
| 2904 | RValue<UShort4> AddSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2905 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2906 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2907 | if(emulateIntrinsics) |
| 2908 | { |
| 2909 | UShort4 result; |
| 2910 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) + Int(Extract(y, 0))), 0); |
| 2911 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) + Int(Extract(y, 1))), 1); |
| 2912 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) + Int(Extract(y, 2))), 2); |
| 2913 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) + Int(Extract(y, 3))), 3); |
| 2914 | |
| 2915 | return result; |
| 2916 | } |
| 2917 | else |
| 2918 | { |
| 2919 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2920 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AddSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2921 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2922 | auto paddusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2923 | paddusw->addArg(x.value()); |
| 2924 | paddusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2925 | ::basicBlock->appendInst(paddusw); |
| 2926 | |
| 2927 | return RValue<UShort4>(V(result)); |
| 2928 | } |
| 2929 | } |
| 2930 | |
| 2931 | RValue<UShort4> SubSat(RValue<UShort4> x, RValue<UShort4> y) |
| 2932 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2933 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2934 | if(emulateIntrinsics) |
| 2935 | { |
| 2936 | UShort4 result; |
| 2937 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 0)) - Int(Extract(y, 0))), 0); |
| 2938 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 1)) - Int(Extract(y, 1))), 1); |
| 2939 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 2)) - Int(Extract(y, 2))), 2); |
| 2940 | result = Insert(result, SaturateUnsigned(Int(Extract(x, 3)) - Int(Extract(y, 3))), 3); |
| 2941 | |
| 2942 | return result; |
| 2943 | } |
| 2944 | else |
| 2945 | { |
| 2946 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2947 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SubtractSaturateUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2948 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2949 | auto psubusw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2950 | psubusw->addArg(x.value()); |
| 2951 | psubusw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2952 | ::basicBlock->appendInst(psubusw); |
| 2953 | |
| 2954 | return RValue<UShort4>(V(result)); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | RValue<UShort4> MulHigh(RValue<UShort4> x, RValue<UShort4> y) |
| 2959 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2960 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2961 | if(emulateIntrinsics) |
| 2962 | { |
| 2963 | UShort4 result; |
| 2964 | result = Insert(result, UShort((UInt(Extract(x, 0)) * UInt(Extract(y, 0))) >> 16), 0); |
| 2965 | result = Insert(result, UShort((UInt(Extract(x, 1)) * UInt(Extract(y, 1))) >> 16), 1); |
| 2966 | result = Insert(result, UShort((UInt(Extract(x, 2)) * UInt(Extract(y, 2))) >> 16), 2); |
| 2967 | result = Insert(result, UShort((UInt(Extract(x, 3)) * UInt(Extract(y, 3))) >> 16), 3); |
| 2968 | |
| 2969 | return result; |
| 2970 | } |
| 2971 | else |
| 2972 | { |
| 2973 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 2974 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::MultiplyHighUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2975 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 2976 | auto pmulhuw = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 2977 | pmulhuw->addArg(x.value()); |
| 2978 | pmulhuw->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2979 | ::basicBlock->appendInst(pmulhuw); |
| 2980 | |
| 2981 | return RValue<UShort4>(V(result)); |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | RValue<Int4> MulHigh(RValue<Int4> x, RValue<Int4> y) |
| 2986 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 2987 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 2988 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 2989 | |
| 2990 | // Scalarized implementation. |
| 2991 | Int4 result; |
| 2992 | result = Insert(result, Int((Long(Extract(x, 0)) * Long(Extract(y, 0))) >> Long(Int(32))), 0); |
| 2993 | result = Insert(result, Int((Long(Extract(x, 1)) * Long(Extract(y, 1))) >> Long(Int(32))), 1); |
| 2994 | result = Insert(result, Int((Long(Extract(x, 2)) * Long(Extract(y, 2))) >> Long(Int(32))), 2); |
| 2995 | result = Insert(result, Int((Long(Extract(x, 3)) * Long(Extract(y, 3))) >> Long(Int(32))), 3); |
| 2996 | |
| 2997 | return result; |
| 2998 | } |
| 2999 | |
| 3000 | RValue<UInt4> MulHigh(RValue<UInt4> x, RValue<UInt4> y) |
| 3001 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3002 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3003 | // TODO: For x86, build an intrinsics version of this which uses shuffles + pmuludq. |
| 3004 | |
| 3005 | if(false) // Partial product based implementation. |
| 3006 | { |
| 3007 | auto xh = x >> 16; |
| 3008 | auto yh = y >> 16; |
| 3009 | auto xl = x & UInt4(0x0000FFFF); |
| 3010 | auto yl = y & UInt4(0x0000FFFF); |
| 3011 | auto xlyh = xl * yh; |
| 3012 | auto xhyl = xh * yl; |
| 3013 | auto xlyhh = xlyh >> 16; |
| 3014 | auto xhylh = xhyl >> 16; |
| 3015 | auto xlyhl = xlyh & UInt4(0x0000FFFF); |
| 3016 | auto xhyll = xhyl & UInt4(0x0000FFFF); |
| 3017 | auto xlylh = (xl * yl) >> 16; |
| 3018 | auto oflow = (xlyhl + xhyll + xlylh) >> 16; |
| 3019 | |
| 3020 | return (xh * yh) + (xlyhh + xhylh) + oflow; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3021 | } |
| 3022 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3023 | // Scalarized implementation. |
| 3024 | Int4 result; |
| 3025 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 0))) * Long(UInt(Extract(As<Int4>(y), 0)))) >> Long(Int(32))), 0); |
| 3026 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 1))) * Long(UInt(Extract(As<Int4>(y), 1)))) >> Long(Int(32))), 1); |
| 3027 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 2))) * Long(UInt(Extract(As<Int4>(y), 2)))) >> Long(Int(32))), 2); |
| 3028 | result = Insert(result, Int((Long(UInt(Extract(As<Int4>(x), 3))) * Long(UInt(Extract(As<Int4>(y), 3)))) >> Long(Int(32))), 3); |
| 3029 | |
| 3030 | return As<UInt4>(result); |
| 3031 | } |
| 3032 | |
| 3033 | RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y) |
| 3034 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3035 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3036 | UNIMPLEMENTED_NO_BUG("RValue<UShort4> Average(RValue<UShort4> x, RValue<UShort4> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3037 | return UShort4(0); |
| 3038 | } |
| 3039 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3040 | Type *UShort4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3041 | { |
| 3042 | return T(Type_v4i16); |
| 3043 | } |
| 3044 | |
| 3045 | RValue<Short> Extract(RValue<Short8> val, int i) |
| 3046 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3047 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3048 | return RValue<Short>(Nucleus::createExtractElement(val.value(), Short::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | RValue<Short8> Insert(RValue<Short8> val, RValue<Short> element, int i) |
| 3052 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3053 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3054 | return RValue<Short8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | RValue<Short8> operator<<(RValue<Short8> lhs, unsigned char rhs) |
| 3058 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3059 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3060 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3061 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3062 | Short8 result; |
| 3063 | result = Insert(result, Extract(lhs, 0) << Short(rhs), 0); |
| 3064 | result = Insert(result, Extract(lhs, 1) << Short(rhs), 1); |
| 3065 | result = Insert(result, Extract(lhs, 2) << Short(rhs), 2); |
| 3066 | result = Insert(result, Extract(lhs, 3) << Short(rhs), 3); |
| 3067 | result = Insert(result, Extract(lhs, 4) << Short(rhs), 4); |
| 3068 | result = Insert(result, Extract(lhs, 5) << Short(rhs), 5); |
| 3069 | result = Insert(result, Extract(lhs, 6) << Short(rhs), 6); |
| 3070 | result = Insert(result, Extract(lhs, 7) << Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3071 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3072 | return result; |
| 3073 | } |
| 3074 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3075 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3076 | return RValue<Short8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3077 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3078 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3079 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3080 | RValue<Short8> operator>>(RValue<Short8> lhs, unsigned char rhs) |
| 3081 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3082 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3083 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3084 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3085 | Short8 result; |
| 3086 | result = Insert(result, Extract(lhs, 0) >> Short(rhs), 0); |
| 3087 | result = Insert(result, Extract(lhs, 1) >> Short(rhs), 1); |
| 3088 | result = Insert(result, Extract(lhs, 2) >> Short(rhs), 2); |
| 3089 | result = Insert(result, Extract(lhs, 3) >> Short(rhs), 3); |
| 3090 | result = Insert(result, Extract(lhs, 4) >> Short(rhs), 4); |
| 3091 | result = Insert(result, Extract(lhs, 5) >> Short(rhs), 5); |
| 3092 | result = Insert(result, Extract(lhs, 6) >> Short(rhs), 6); |
| 3093 | result = Insert(result, Extract(lhs, 7) >> Short(rhs), 7); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3094 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3095 | return result; |
| 3096 | } |
| 3097 | else |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3098 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3099 | return RValue<Short8>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3100 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3101 | } |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3102 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3103 | RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y) |
| 3104 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3105 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3106 | UNIMPLEMENTED_NO_BUG("RValue<Int4> MulAdd(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3107 | return Int4(0); |
| 3108 | } |
| 3109 | |
| 3110 | RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y) |
| 3111 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3112 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3113 | UNIMPLEMENTED_NO_BUG("RValue<Short8> MulHigh(RValue<Short8> x, RValue<Short8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3114 | return Short8(0); |
| 3115 | } |
| 3116 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3117 | Type *Short8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3118 | { |
| 3119 | return T(Ice::IceType_v8i16); |
| 3120 | } |
| 3121 | |
| 3122 | RValue<UShort> Extract(RValue<UShort8> val, int i) |
| 3123 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3124 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3125 | return RValue<UShort>(Nucleus::createExtractElement(val.value(), UShort::type(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | RValue<UShort8> Insert(RValue<UShort8> val, RValue<UShort> element, int i) |
| 3129 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3130 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3131 | return RValue<UShort8>(Nucleus::createInsertElement(val.value(), element.value(), i)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3132 | } |
| 3133 | |
| 3134 | RValue<UShort8> operator<<(RValue<UShort8> lhs, unsigned char rhs) |
| 3135 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3136 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3137 | if(emulateIntrinsics) |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3138 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3139 | UShort8 result; |
| 3140 | result = Insert(result, Extract(lhs, 0) << UShort(rhs), 0); |
| 3141 | result = Insert(result, Extract(lhs, 1) << UShort(rhs), 1); |
| 3142 | result = Insert(result, Extract(lhs, 2) << UShort(rhs), 2); |
| 3143 | result = Insert(result, Extract(lhs, 3) << UShort(rhs), 3); |
| 3144 | result = Insert(result, Extract(lhs, 4) << UShort(rhs), 4); |
| 3145 | result = Insert(result, Extract(lhs, 5) << UShort(rhs), 5); |
| 3146 | result = Insert(result, Extract(lhs, 6) << UShort(rhs), 6); |
| 3147 | result = Insert(result, Extract(lhs, 7) << UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3148 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3149 | return result; |
| 3150 | } |
| 3151 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3152 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3153 | return RValue<UShort8>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3154 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3155 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3156 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3157 | RValue<UShort8> operator>>(RValue<UShort8> lhs, unsigned char rhs) |
| 3158 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3159 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3160 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3161 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3162 | UShort8 result; |
| 3163 | result = Insert(result, Extract(lhs, 0) >> UShort(rhs), 0); |
| 3164 | result = Insert(result, Extract(lhs, 1) >> UShort(rhs), 1); |
| 3165 | result = Insert(result, Extract(lhs, 2) >> UShort(rhs), 2); |
| 3166 | result = Insert(result, Extract(lhs, 3) >> UShort(rhs), 3); |
| 3167 | result = Insert(result, Extract(lhs, 4) >> UShort(rhs), 4); |
| 3168 | result = Insert(result, Extract(lhs, 5) >> UShort(rhs), 5); |
| 3169 | result = Insert(result, Extract(lhs, 6) >> UShort(rhs), 6); |
| 3170 | result = Insert(result, Extract(lhs, 7) >> UShort(rhs), 7); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3171 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3172 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3173 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3174 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3175 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3176 | return RValue<UShort8>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3177 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3178 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3179 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3180 | RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y) |
| 3181 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3182 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 3183 | UNIMPLEMENTED_NO_BUG("RValue<UShort8> MulHigh(RValue<UShort8> x, RValue<UShort8> y)"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3184 | return UShort8(0); |
| 3185 | } |
| 3186 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3187 | Type *UShort8::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3188 | { |
| 3189 | return T(Ice::IceType_v8i16); |
| 3190 | } |
| 3191 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3192 | RValue<Int> operator++(Int &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3193 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3194 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3195 | RValue<Int> res = val; |
| 3196 | val += 1; |
| 3197 | return res; |
| 3198 | } |
| 3199 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3200 | const Int &operator++(Int &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3201 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3202 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3203 | val += 1; |
| 3204 | return val; |
| 3205 | } |
| 3206 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3207 | RValue<Int> operator--(Int &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3208 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3209 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3210 | RValue<Int> res = val; |
| 3211 | val -= 1; |
| 3212 | return res; |
| 3213 | } |
| 3214 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3215 | const Int &operator--(Int &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3216 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3217 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3218 | val -= 1; |
| 3219 | return val; |
| 3220 | } |
| 3221 | |
| 3222 | RValue<Int> RoundInt(RValue<Float> cast) |
| 3223 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3224 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3225 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3226 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3227 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3228 | return Int((cast + Float(0x00C00000)) - Float(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3229 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3230 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3231 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3232 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3233 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3234 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3235 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3236 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3237 | ::basicBlock->appendInst(nearbyint); |
| 3238 | |
| 3239 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3240 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3241 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3242 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3243 | Type *Int::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3244 | { |
| 3245 | return T(Ice::IceType_i32); |
| 3246 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3247 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3248 | Type *Long::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3249 | { |
| 3250 | return T(Ice::IceType_i64); |
| 3251 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3252 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3253 | UInt::UInt(RValue<Float> cast) |
| 3254 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3255 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3256 | // Smallest positive value representable in UInt, but not in Int |
| 3257 | const unsigned int ustart = 0x80000000u; |
| 3258 | const float ustartf = float(ustart); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3259 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3260 | // If the value is negative, store 0, otherwise store the result of the conversion |
| 3261 | storeValue((~(As<Int>(cast) >> 31) & |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3262 | // Check if the value can be represented as an Int |
| 3263 | IfThenElse(cast >= ustartf, |
| 3264 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3265 | As<Int>(As<UInt>(Int(cast - Float(ustartf))) + UInt(ustart)), |
| 3266 | // Otherwise, just convert normally |
| 3267 | Int(cast))) |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3268 | .value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3269 | } |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3270 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3271 | RValue<UInt> operator++(UInt &val, int) // Post-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3272 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3273 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3274 | RValue<UInt> res = val; |
| 3275 | val += 1; |
| 3276 | return res; |
| 3277 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3278 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3279 | const UInt &operator++(UInt &val) // Pre-increment |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3280 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3281 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3282 | val += 1; |
| 3283 | return val; |
| 3284 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3285 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3286 | RValue<UInt> operator--(UInt &val, int) // Post-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3287 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3288 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3289 | RValue<UInt> res = val; |
| 3290 | val -= 1; |
| 3291 | return res; |
| 3292 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3293 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3294 | const UInt &operator--(UInt &val) // Pre-decrement |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3295 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3296 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3297 | val -= 1; |
| 3298 | return val; |
| 3299 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3300 | |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3301 | // RValue<UInt> RoundUInt(RValue<Float> cast) |
| 3302 | // { |
Ben Clayton | eb50d25 | 2019-04-15 13:50:01 -0400 | [diff] [blame] | 3303 | // ASSERT(false && "UNIMPLEMENTED"); return RValue<UInt>(V(nullptr)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3304 | // } |
| 3305 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3306 | Type *UInt::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3307 | { |
| 3308 | return T(Ice::IceType_i32); |
| 3309 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3310 | |
| 3311 | // Int2::Int2(RValue<Int> cast) |
| 3312 | // { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3313 | // Value *extend = Nucleus::createZExt(cast.value(), Long::type()); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3314 | // Value *vector = Nucleus::createBitCast(extend, Int2::type()); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3315 | // |
| 3316 | // Constant *shuffle[2]; |
| 3317 | // shuffle[0] = Nucleus::createConstantInt(0); |
| 3318 | // shuffle[1] = Nucleus::createConstantInt(0); |
| 3319 | // |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3320 | // Value *replicate = Nucleus::createShuffleVector(vector, UndefValue::get(Int2::type()), Nucleus::createConstantVector(shuffle, 2)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3321 | // |
| 3322 | // storeValue(replicate); |
| 3323 | // } |
| 3324 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3325 | RValue<Int2> operator<<(RValue<Int2> lhs, unsigned char rhs) |
| 3326 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3327 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3328 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3329 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3330 | Int2 result; |
| 3331 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3332 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3333 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3334 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3335 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3336 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3337 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3338 | return RValue<Int2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3339 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3340 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3341 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3342 | RValue<Int2> operator>>(RValue<Int2> lhs, unsigned char rhs) |
| 3343 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3344 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3345 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3346 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3347 | Int2 result; |
| 3348 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3349 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3350 | |
| 3351 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3352 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3353 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3354 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3355 | return RValue<Int2>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3356 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3357 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3358 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3359 | Type *Int2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3360 | { |
| 3361 | return T(Type_v2i32); |
| 3362 | } |
| 3363 | |
| 3364 | RValue<UInt2> operator<<(RValue<UInt2> lhs, unsigned char rhs) |
| 3365 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3366 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3367 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3368 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3369 | UInt2 result; |
| 3370 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3371 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3372 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3373 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3374 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3375 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3376 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3377 | return RValue<UInt2>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3378 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3379 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3380 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3381 | RValue<UInt2> operator>>(RValue<UInt2> lhs, unsigned char rhs) |
| 3382 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3383 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3384 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3385 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3386 | UInt2 result; |
| 3387 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3388 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3389 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3390 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3391 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3392 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3393 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3394 | return RValue<UInt2>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3395 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3396 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3397 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3398 | Type *UInt2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3399 | { |
| 3400 | return T(Type_v2i32); |
| 3401 | } |
| 3402 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3403 | Int4::Int4(RValue<Byte4> cast) |
| 3404 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3405 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3406 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3407 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3408 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3409 | |
| 3410 | Value *e; |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3411 | int swizzle[16] = { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3412 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
| 3413 | Value *c = Nucleus::createShuffleVector(b, Nucleus::createNullValue(Byte16::type()), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3414 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3415 | int swizzle2[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3416 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
| 3417 | e = Nucleus::createShuffleVector(d, Nucleus::createNullValue(Short8::type()), swizzle2); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3418 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3419 | Value *f = Nucleus::createBitCast(e, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3420 | storeValue(f); |
| 3421 | } |
| 3422 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3423 | Int4::Int4(RValue<SByte4> cast) |
| 3424 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3425 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3426 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3427 | Value *x = Nucleus::createBitCast(cast.value(), Int::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3428 | Value *a = Nucleus::createInsertElement(loadValue(), x, 0); |
| 3429 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3430 | int swizzle[16] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3431 | Value *b = Nucleus::createBitCast(a, Byte16::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3432 | Value *c = Nucleus::createShuffleVector(b, b, swizzle); |
| 3433 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3434 | int swizzle2[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3435 | Value *d = Nucleus::createBitCast(c, Short8::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3436 | Value *e = Nucleus::createShuffleVector(d, d, swizzle2); |
| 3437 | |
| 3438 | *this = As<Int4>(e) >> 24; |
| 3439 | } |
| 3440 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3441 | Int4::Int4(RValue<Short4> cast) |
| 3442 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3443 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3444 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3445 | int swizzle[8] = { 0, 0, 1, 1, 2, 2, 3, 3 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3446 | Value *c = Nucleus::createShuffleVector(cast.value(), cast.value(), swizzle); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3447 | |
| 3448 | *this = As<Int4>(c) >> 16; |
| 3449 | } |
| 3450 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3451 | Int4::Int4(RValue<UShort4> cast) |
| 3452 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3453 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3454 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3455 | int swizzle[8] = { 0, 8, 1, 9, 2, 10, 3, 11 }; |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3456 | Value *c = Nucleus::createShuffleVector(cast.value(), Short8(0, 0, 0, 0, 0, 0, 0, 0).loadValue(), swizzle); |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3457 | Value *d = Nucleus::createBitCast(c, Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3458 | storeValue(d); |
| 3459 | } |
| 3460 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3461 | Int4::Int4(RValue<Int> rhs) |
| 3462 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3463 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3464 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3465 | Value *vector = Nucleus::createBitCast(rhs.value(), Int4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3466 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3467 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3468 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3469 | |
| 3470 | storeValue(replicate); |
| 3471 | } |
| 3472 | |
| 3473 | RValue<Int4> operator<<(RValue<Int4> lhs, unsigned char rhs) |
| 3474 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3475 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3476 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3477 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3478 | Int4 result; |
| 3479 | result = Insert(result, Extract(lhs, 0) << Int(rhs), 0); |
| 3480 | result = Insert(result, Extract(lhs, 1) << Int(rhs), 1); |
| 3481 | result = Insert(result, Extract(lhs, 2) << Int(rhs), 2); |
| 3482 | result = Insert(result, Extract(lhs, 3) << Int(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3483 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3484 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3485 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3486 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3487 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3488 | return RValue<Int4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3489 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3490 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3491 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3492 | RValue<Int4> operator>>(RValue<Int4> lhs, unsigned char rhs) |
| 3493 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3494 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3495 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3496 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3497 | Int4 result; |
| 3498 | result = Insert(result, Extract(lhs, 0) >> Int(rhs), 0); |
| 3499 | result = Insert(result, Extract(lhs, 1) >> Int(rhs), 1); |
| 3500 | result = Insert(result, Extract(lhs, 2) >> Int(rhs), 2); |
| 3501 | result = Insert(result, Extract(lhs, 3) >> Int(rhs), 3); |
Nicolas Capens | d422796 | 2016-11-09 14:24:25 -0500 | [diff] [blame] | 3502 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3503 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3504 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3505 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3506 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3507 | return RValue<Int4>(Nucleus::createAShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3508 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3509 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3510 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3511 | RValue<Int4> CmpEQ(RValue<Int4> x, RValue<Int4> y) |
| 3512 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3513 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3514 | return RValue<Int4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3515 | } |
| 3516 | |
| 3517 | RValue<Int4> CmpLT(RValue<Int4> x, RValue<Int4> y) |
| 3518 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3519 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3520 | return RValue<Int4>(Nucleus::createICmpSLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3521 | } |
| 3522 | |
| 3523 | RValue<Int4> CmpLE(RValue<Int4> x, RValue<Int4> y) |
| 3524 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3525 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3526 | return RValue<Int4>(Nucleus::createICmpSLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3527 | } |
| 3528 | |
| 3529 | RValue<Int4> CmpNEQ(RValue<Int4> x, RValue<Int4> y) |
| 3530 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3531 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3532 | return RValue<Int4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | RValue<Int4> CmpNLT(RValue<Int4> x, RValue<Int4> y) |
| 3536 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3537 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3538 | return RValue<Int4>(Nucleus::createICmpSGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | RValue<Int4> CmpNLE(RValue<Int4> x, RValue<Int4> y) |
| 3542 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3543 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3544 | return RValue<Int4>(Nucleus::createICmpSGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | RValue<Int4> Max(RValue<Int4> x, RValue<Int4> y) |
| 3548 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3549 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3550 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3551 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sle, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3552 | ::basicBlock->appendInst(cmp); |
| 3553 | |
| 3554 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3555 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3556 | ::basicBlock->appendInst(select); |
| 3557 | |
| 3558 | return RValue<Int4>(V(result)); |
| 3559 | } |
| 3560 | |
| 3561 | RValue<Int4> Min(RValue<Int4> x, RValue<Int4> y) |
| 3562 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3563 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3564 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3565 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Sgt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3566 | ::basicBlock->appendInst(cmp); |
| 3567 | |
| 3568 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3569 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3570 | ::basicBlock->appendInst(select); |
| 3571 | |
| 3572 | return RValue<Int4>(V(result)); |
| 3573 | } |
| 3574 | |
| 3575 | RValue<Int4> RoundInt(RValue<Float4> cast) |
| 3576 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3577 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3578 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3579 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3580 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3581 | return Int4((cast + Float4(0x00C00000)) - Float4(0x00C00000)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3582 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3583 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3584 | { |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3585 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3586 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3587 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3588 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3589 | nearbyint->addArg(cast.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3590 | ::basicBlock->appendInst(nearbyint); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3591 | |
| 3592 | return RValue<Int4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3593 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3594 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3595 | |
Nicolas Capens | eeb8184 | 2021-01-12 17:44:40 -0500 | [diff] [blame] | 3596 | RValue<Int4> RoundIntClamped(RValue<Float4> cast) |
| 3597 | { |
| 3598 | RR_DEBUG_INFO_UPDATE_LOC(); |
| 3599 | |
| 3600 | // cvtps2dq produces 0x80000000, a negative value, for input larger than |
| 3601 | // 2147483520.0, so clamp to 2147483520. Values less than -2147483520.0 |
| 3602 | // saturate to 0x80000000. |
| 3603 | RValue<Float4> clamped = Min(cast, Float4(0x7FFFFF80)); |
| 3604 | |
| 3605 | if(emulateIntrinsics || CPUID::ARM) |
| 3606 | { |
| 3607 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 3608 | return Int4((clamped + Float4(0x00C00000)) - Float4(0x00C00000)); |
| 3609 | } |
| 3610 | else |
| 3611 | { |
| 3612 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
| 3613 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Nearbyint, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 3614 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3615 | auto nearbyint = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
| 3616 | nearbyint->addArg(clamped.value()); |
| 3617 | ::basicBlock->appendInst(nearbyint); |
| 3618 | |
| 3619 | return RValue<Int4>(V(result)); |
| 3620 | } |
| 3621 | } |
| 3622 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3623 | RValue<Short8> PackSigned(RValue<Int4> x, RValue<Int4> y) |
| 3624 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3625 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3626 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3627 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3628 | Short8 result; |
| 3629 | result = Insert(result, SaturateSigned(Extract(x, 0)), 0); |
| 3630 | result = Insert(result, SaturateSigned(Extract(x, 1)), 1); |
| 3631 | result = Insert(result, SaturateSigned(Extract(x, 2)), 2); |
| 3632 | result = Insert(result, SaturateSigned(Extract(x, 3)), 3); |
| 3633 | result = Insert(result, SaturateSigned(Extract(y, 0)), 4); |
| 3634 | result = Insert(result, SaturateSigned(Extract(y, 1)), 5); |
| 3635 | result = Insert(result, SaturateSigned(Extract(y, 2)), 6); |
| 3636 | result = Insert(result, SaturateSigned(Extract(y, 3)), 7); |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 3637 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3638 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3639 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3640 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3641 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3642 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3643 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackSigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3644 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3645 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3646 | pack->addArg(x.value()); |
| 3647 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3648 | ::basicBlock->appendInst(pack); |
Nicolas Capens | a808651 | 2016-11-07 17:32:17 -0500 | [diff] [blame] | 3649 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3650 | return RValue<Short8>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3651 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3652 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3653 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3654 | RValue<UShort8> PackUnsigned(RValue<Int4> x, RValue<Int4> y) |
| 3655 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3656 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3657 | if(emulateIntrinsics || !(CPUID::SSE4_1 || CPUID::ARM)) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3658 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3659 | RValue<Int4> sx = As<Int4>(x); |
| 3660 | RValue<Int4> bx = (sx & ~(sx >> 31)) - Int4(0x8000); |
Nicolas Capens | ec54a17 | 2016-10-25 17:32:37 -0400 | [diff] [blame] | 3661 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3662 | RValue<Int4> sy = As<Int4>(y); |
| 3663 | RValue<Int4> by = (sy & ~(sy >> 31)) - Int4(0x8000); |
Nicolas Capens | 8960fbf | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3664 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3665 | return As<UShort8>(PackSigned(bx, by) + Short8(0x8000u)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3666 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3667 | else |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3668 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3669 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v8i16); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3670 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::VectorPackUnsigned, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3671 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3672 | auto pack = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3673 | pack->addArg(x.value()); |
| 3674 | pack->addArg(y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3675 | ::basicBlock->appendInst(pack); |
Nicolas Capens | 091f350 | 2017-10-03 14:56:49 -0400 | [diff] [blame] | 3676 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3677 | return RValue<UShort8>(V(result)); |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3678 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3679 | } |
Nicolas Capens | 33438a6 | 2017-09-27 11:47:35 -0400 | [diff] [blame] | 3680 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3681 | RValue<Int> SignMask(RValue<Int4> x) |
| 3682 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3683 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3684 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3685 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3686 | Int4 xx = (x >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 3687 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3688 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3689 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3690 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3691 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3692 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3693 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3694 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3695 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3696 | ::basicBlock->appendInst(movmsk); |
| 3697 | |
| 3698 | return RValue<Int>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3699 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3700 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3701 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3702 | Type *Int4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3703 | { |
| 3704 | return T(Ice::IceType_v4i32); |
| 3705 | } |
| 3706 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3707 | UInt4::UInt4(RValue<Float4> cast) |
| 3708 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3709 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3710 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3711 | // Smallest positive value representable in UInt, but not in Int |
| 3712 | const unsigned int ustart = 0x80000000u; |
| 3713 | const float ustartf = float(ustart); |
| 3714 | |
| 3715 | // Check if the value can be represented as an Int |
| 3716 | Int4 uiValue = CmpNLT(cast, Float4(ustartf)); |
| 3717 | // If the value is too large, subtract ustart and re-add it after conversion. |
| 3718 | uiValue = (uiValue & As<Int4>(As<UInt4>(Int4(cast - Float4(ustartf))) + UInt4(ustart))) | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3719 | // Otherwise, just convert normally |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3720 | (~uiValue & Int4(cast)); |
| 3721 | // If the value is negative, store 0, otherwise store the result of the conversion |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3722 | storeValue((~(As<Int4>(cast) >> 31) & uiValue).value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3723 | } |
| 3724 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3725 | UInt4::UInt4(RValue<UInt> rhs) |
| 3726 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3727 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3728 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3729 | Value *vector = Nucleus::createBitCast(rhs.value(), UInt4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3730 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3731 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3732 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3733 | |
| 3734 | storeValue(replicate); |
| 3735 | } |
| 3736 | |
| 3737 | RValue<UInt4> operator<<(RValue<UInt4> lhs, unsigned char rhs) |
| 3738 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3739 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3740 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3741 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3742 | UInt4 result; |
| 3743 | result = Insert(result, Extract(lhs, 0) << UInt(rhs), 0); |
| 3744 | result = Insert(result, Extract(lhs, 1) << UInt(rhs), 1); |
| 3745 | result = Insert(result, Extract(lhs, 2) << UInt(rhs), 2); |
| 3746 | result = Insert(result, Extract(lhs, 3) << UInt(rhs), 3); |
Nicolas Capens | c70a116 | 2016-12-03 00:16:14 -0500 | [diff] [blame] | 3747 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3748 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3749 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3750 | else |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3751 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3752 | return RValue<UInt4>(Nucleus::createShl(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3753 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3754 | } |
Ben Clayton | 88816fa | 2019-05-15 17:08:14 +0100 | [diff] [blame] | 3755 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3756 | RValue<UInt4> operator>>(RValue<UInt4> lhs, unsigned char rhs) |
| 3757 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3758 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3759 | if(emulateIntrinsics) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3760 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3761 | UInt4 result; |
| 3762 | result = Insert(result, Extract(lhs, 0) >> UInt(rhs), 0); |
| 3763 | result = Insert(result, Extract(lhs, 1) >> UInt(rhs), 1); |
| 3764 | result = Insert(result, Extract(lhs, 2) >> UInt(rhs), 2); |
| 3765 | result = Insert(result, Extract(lhs, 3) >> UInt(rhs), 3); |
Nicolas Capens | 8be6c7b | 2017-07-25 15:32:12 -0400 | [diff] [blame] | 3766 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3767 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3768 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3769 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3770 | { |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3771 | return RValue<UInt4>(Nucleus::createLShr(lhs.value(), V(::context->getConstantInt32(rhs)))); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3772 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3773 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 3774 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3775 | RValue<UInt4> CmpEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3776 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3777 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3778 | return RValue<UInt4>(Nucleus::createICmpEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | RValue<UInt4> CmpLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3782 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3783 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3784 | return RValue<UInt4>(Nucleus::createICmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3785 | } |
| 3786 | |
| 3787 | RValue<UInt4> CmpLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3788 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3789 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3790 | return RValue<UInt4>(Nucleus::createICmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | RValue<UInt4> CmpNEQ(RValue<UInt4> x, RValue<UInt4> y) |
| 3794 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3795 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3796 | return RValue<UInt4>(Nucleus::createICmpNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | RValue<UInt4> CmpNLT(RValue<UInt4> x, RValue<UInt4> y) |
| 3800 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3801 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3802 | return RValue<UInt4>(Nucleus::createICmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | RValue<UInt4> CmpNLE(RValue<UInt4> x, RValue<UInt4> y) |
| 3806 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3807 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3808 | return RValue<UInt4>(Nucleus::createICmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | RValue<UInt4> Max(RValue<UInt4> x, RValue<UInt4> y) |
| 3812 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3813 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3814 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3815 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ule, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3816 | ::basicBlock->appendInst(cmp); |
| 3817 | |
| 3818 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3819 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3820 | ::basicBlock->appendInst(select); |
| 3821 | |
| 3822 | return RValue<UInt4>(V(result)); |
| 3823 | } |
| 3824 | |
| 3825 | RValue<UInt4> Min(RValue<UInt4> x, RValue<UInt4> y) |
| 3826 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3827 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3828 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3829 | auto cmp = Ice::InstIcmp::create(::function, Ice::InstIcmp::Ugt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3830 | ::basicBlock->appendInst(cmp); |
| 3831 | |
| 3832 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4i32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3833 | auto select = Ice::InstSelect::create(::function, result, condition, y.value(), x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3834 | ::basicBlock->appendInst(select); |
| 3835 | |
| 3836 | return RValue<UInt4>(V(result)); |
| 3837 | } |
| 3838 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3839 | Type *UInt4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3840 | { |
| 3841 | return T(Ice::IceType_v4i32); |
| 3842 | } |
| 3843 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3844 | Type *Half::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3845 | { |
| 3846 | return T(Ice::IceType_i16); |
| 3847 | } |
| 3848 | |
| 3849 | RValue<Float> Rcp_pp(RValue<Float> x, bool exactAtPow2) |
| 3850 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3851 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3852 | return 1.0f / x; |
| 3853 | } |
| 3854 | |
| 3855 | RValue<Float> RcpSqrt_pp(RValue<Float> x) |
| 3856 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3857 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3858 | return Rcp_pp(Sqrt(x)); |
| 3859 | } |
| 3860 | |
| 3861 | RValue<Float> Sqrt(RValue<Float> x) |
| 3862 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3863 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3864 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3865 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3866 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 3867 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3868 | sqrt->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3869 | ::basicBlock->appendInst(sqrt); |
| 3870 | |
| 3871 | return RValue<Float>(V(result)); |
| 3872 | } |
| 3873 | |
| 3874 | RValue<Float> Round(RValue<Float> x) |
| 3875 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3876 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3877 | return Float4(Round(Float4(x))).x; |
| 3878 | } |
| 3879 | |
| 3880 | RValue<Float> Trunc(RValue<Float> x) |
| 3881 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3882 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3883 | return Float4(Trunc(Float4(x))).x; |
| 3884 | } |
| 3885 | |
| 3886 | RValue<Float> Frac(RValue<Float> x) |
| 3887 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3888 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3889 | return Float4(Frac(Float4(x))).x; |
| 3890 | } |
| 3891 | |
| 3892 | RValue<Float> Floor(RValue<Float> x) |
| 3893 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3894 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3895 | return Float4(Floor(Float4(x))).x; |
| 3896 | } |
| 3897 | |
| 3898 | RValue<Float> Ceil(RValue<Float> x) |
| 3899 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3900 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3901 | return Float4(Ceil(Float4(x))).x; |
| 3902 | } |
| 3903 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3904 | Type *Float::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3905 | { |
| 3906 | return T(Ice::IceType_f32); |
| 3907 | } |
| 3908 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 3909 | Type *Float2::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3910 | { |
| 3911 | return T(Type_v2f32); |
| 3912 | } |
| 3913 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3914 | Float4::Float4(RValue<Float> rhs) |
| 3915 | : XYZW(this) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3916 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3917 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3918 | Value *vector = Nucleus::createBitCast(rhs.value(), Float4::type()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3919 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 3920 | int swizzle[4] = { 0, 0, 0, 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3921 | Value *replicate = Nucleus::createShuffleVector(vector, vector, swizzle); |
| 3922 | |
| 3923 | storeValue(replicate); |
| 3924 | } |
| 3925 | |
| 3926 | RValue<Float4> Max(RValue<Float4> x, RValue<Float4> y) |
| 3927 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3928 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3929 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3930 | auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Ogt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3931 | ::basicBlock->appendInst(cmp); |
| 3932 | |
| 3933 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3934 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3935 | ::basicBlock->appendInst(select); |
| 3936 | |
| 3937 | return RValue<Float4>(V(result)); |
| 3938 | } |
| 3939 | |
| 3940 | RValue<Float4> Min(RValue<Float4> x, RValue<Float4> y) |
| 3941 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3942 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3943 | Ice::Variable *condition = ::function->makeVariable(Ice::IceType_v4i1); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3944 | auto cmp = Ice::InstFcmp::create(::function, Ice::InstFcmp::Olt, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3945 | ::basicBlock->appendInst(cmp); |
| 3946 | |
| 3947 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 3948 | auto select = Ice::InstSelect::create(::function, result, condition, x.value(), y.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3949 | ::basicBlock->appendInst(select); |
| 3950 | |
| 3951 | return RValue<Float4>(V(result)); |
| 3952 | } |
| 3953 | |
| 3954 | RValue<Float4> Rcp_pp(RValue<Float4> x, bool exactAtPow2) |
| 3955 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3956 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3957 | return Float4(1.0f) / x; |
| 3958 | } |
| 3959 | |
| 3960 | RValue<Float4> RcpSqrt_pp(RValue<Float4> x) |
| 3961 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 3962 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 3963 | return Rcp_pp(Sqrt(x)); |
| 3964 | } |
| 3965 | |
Antonio Maiorano | d156187 | 2020-12-14 14:03:53 -0500 | [diff] [blame] | 3966 | bool HasRcpApprox() |
| 3967 | { |
| 3968 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3969 | return false; |
| 3970 | } |
| 3971 | |
| 3972 | RValue<Float4> RcpApprox(RValue<Float4> x, bool exactAtPow2) |
| 3973 | { |
| 3974 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3975 | UNREACHABLE("RValue<Float4> RcpApprox()"); |
| 3976 | return { 0.0f }; |
| 3977 | } |
| 3978 | |
| 3979 | RValue<Float> RcpApprox(RValue<Float> x, bool exactAtPow2) |
| 3980 | { |
| 3981 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3982 | UNREACHABLE("RValue<Float> RcpApprox()"); |
| 3983 | return { 0.0f }; |
| 3984 | } |
| 3985 | |
Antonio Maiorano | 1cc5b33 | 2020-12-14 16:57:28 -0500 | [diff] [blame] | 3986 | bool HasRcpSqrtApprox() |
| 3987 | { |
| 3988 | return false; |
| 3989 | } |
| 3990 | |
| 3991 | RValue<Float4> RcpSqrtApprox(RValue<Float4> x) |
| 3992 | { |
| 3993 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 3994 | UNREACHABLE("RValue<Float4> RcpSqrtApprox()"); |
| 3995 | return { 0.0f }; |
| 3996 | } |
| 3997 | |
| 3998 | RValue<Float> RcpSqrtApprox(RValue<Float> x) |
| 3999 | { |
| 4000 | // TODO(b/175612820): Update once we implement x86 SSE rcp_ss and rsqrt_ss intrinsics in Subzero |
| 4001 | UNREACHABLE("RValue<Float> RcpSqrtApprox()"); |
| 4002 | return { 0.0f }; |
| 4003 | } |
| 4004 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4005 | RValue<Float4> Sqrt(RValue<Float4> x) |
| 4006 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4007 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4008 | if(emulateIntrinsics || CPUID::ARM) |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4009 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4010 | Float4 result; |
| 4011 | result.x = Sqrt(Float(Float4(x).x)); |
| 4012 | result.y = Sqrt(Float(Float4(x).y)); |
| 4013 | result.z = Sqrt(Float(Float4(x).z)); |
| 4014 | result.w = Sqrt(Float(Float4(x).w)); |
| 4015 | |
| 4016 | return result; |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4017 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4018 | else |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4019 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4020 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4021 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Sqrt, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4022 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4023 | auto sqrt = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4024 | sqrt->addArg(x.value()); |
Nicolas Capens | d52e936 | 2016-10-31 23:23:15 -0400 | [diff] [blame] | 4025 | ::basicBlock->appendInst(sqrt); |
| 4026 | |
Nicolas Capens | 53a8a3f | 2016-10-26 00:23:12 -0400 | [diff] [blame] | 4027 | return RValue<Float4>(V(result)); |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4028 | } |
Nicolas Capens | 598f8d8 | 2016-09-26 15:09:10 -0400 | [diff] [blame] | 4029 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4030 | |
| 4031 | RValue<Int> SignMask(RValue<Float4> x) |
| 4032 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4033 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4034 | if(emulateIntrinsics || CPUID::ARM) |
| 4035 | { |
| 4036 | Int4 xx = (As<Int4>(x) >> 31) & Int4(0x00000001, 0x00000002, 0x00000004, 0x00000008); |
| 4037 | return Extract(xx, 0) | Extract(xx, 1) | Extract(xx, 2) | Extract(xx, 3); |
| 4038 | } |
| 4039 | else |
| 4040 | { |
| 4041 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4042 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::SignMask, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4043 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4044 | auto movmsk = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4045 | movmsk->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4046 | ::basicBlock->appendInst(movmsk); |
| 4047 | |
| 4048 | return RValue<Int>(V(result)); |
| 4049 | } |
| 4050 | } |
| 4051 | |
| 4052 | RValue<Int4> CmpEQ(RValue<Float4> x, RValue<Float4> y) |
| 4053 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4054 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4055 | return RValue<Int4>(Nucleus::createFCmpOEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | RValue<Int4> CmpLT(RValue<Float4> x, RValue<Float4> y) |
| 4059 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4060 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4061 | return RValue<Int4>(Nucleus::createFCmpOLT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | RValue<Int4> CmpLE(RValue<Float4> x, RValue<Float4> y) |
| 4065 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4066 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4067 | return RValue<Int4>(Nucleus::createFCmpOLE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | RValue<Int4> CmpNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4071 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4072 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4073 | return RValue<Int4>(Nucleus::createFCmpONE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4074 | } |
| 4075 | |
| 4076 | RValue<Int4> CmpNLT(RValue<Float4> x, RValue<Float4> y) |
| 4077 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4078 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4079 | return RValue<Int4>(Nucleus::createFCmpOGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | RValue<Int4> CmpNLE(RValue<Float4> x, RValue<Float4> y) |
| 4083 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4084 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4085 | return RValue<Int4>(Nucleus::createFCmpOGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | RValue<Int4> CmpUEQ(RValue<Float4> x, RValue<Float4> y) |
| 4089 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4090 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4091 | return RValue<Int4>(Nucleus::createFCmpUEQ(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | RValue<Int4> CmpULT(RValue<Float4> x, RValue<Float4> y) |
| 4095 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4096 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4097 | return RValue<Int4>(Nucleus::createFCmpULT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | RValue<Int4> CmpULE(RValue<Float4> x, RValue<Float4> y) |
| 4101 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4102 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4103 | return RValue<Int4>(Nucleus::createFCmpULE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | RValue<Int4> CmpUNEQ(RValue<Float4> x, RValue<Float4> y) |
| 4107 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4108 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4109 | return RValue<Int4>(Nucleus::createFCmpUNE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4110 | } |
| 4111 | |
| 4112 | RValue<Int4> CmpUNLT(RValue<Float4> x, RValue<Float4> y) |
| 4113 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4114 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4115 | return RValue<Int4>(Nucleus::createFCmpUGE(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4116 | } |
| 4117 | |
| 4118 | RValue<Int4> CmpUNLE(RValue<Float4> x, RValue<Float4> y) |
| 4119 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4120 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4121 | return RValue<Int4>(Nucleus::createFCmpUGT(x.value(), y.value())); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4122 | } |
| 4123 | |
| 4124 | RValue<Float4> Round(RValue<Float4> x) |
| 4125 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4126 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4127 | if(emulateIntrinsics || CPUID::ARM) |
| 4128 | { |
| 4129 | // Push the fractional part off the mantissa. Accurate up to +/-2^22. |
| 4130 | return (x + Float4(0x00C00000)) - Float4(0x00C00000); |
| 4131 | } |
| 4132 | else if(CPUID::SSE4_1) |
| 4133 | { |
| 4134 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4135 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4136 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4137 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4138 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4139 | round->addArg(::context->getConstantInt32(0)); |
| 4140 | ::basicBlock->appendInst(round); |
| 4141 | |
| 4142 | return RValue<Float4>(V(result)); |
| 4143 | } |
| 4144 | else |
| 4145 | { |
| 4146 | return Float4(RoundInt(x)); |
| 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | RValue<Float4> Trunc(RValue<Float4> x) |
| 4151 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4152 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4153 | if(CPUID::SSE4_1) |
| 4154 | { |
| 4155 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4156 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4157 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4158 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4159 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4160 | round->addArg(::context->getConstantInt32(3)); |
| 4161 | ::basicBlock->appendInst(round); |
| 4162 | |
| 4163 | return RValue<Float4>(V(result)); |
| 4164 | } |
| 4165 | else |
| 4166 | { |
| 4167 | return Float4(Int4(x)); |
| 4168 | } |
| 4169 | } |
| 4170 | |
| 4171 | RValue<Float4> Frac(RValue<Float4> x) |
| 4172 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4173 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4174 | Float4 frc; |
| 4175 | |
| 4176 | if(CPUID::SSE4_1) |
| 4177 | { |
| 4178 | frc = x - Floor(x); |
| 4179 | } |
| 4180 | else |
| 4181 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4182 | frc = x - Float4(Int4(x)); // Signed fractional part. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4183 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4184 | frc += As<Float4>(As<Int4>(CmpNLE(Float4(0.0f), frc)) & As<Int4>(Float4(1, 1, 1, 1))); // Add 1.0 if negative. |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | // x - floor(x) can be 1.0 for very small negative x. |
| 4188 | // Clamp against the value just below 1.0. |
| 4189 | return Min(frc, As<Float4>(Int4(0x3F7FFFFF))); |
| 4190 | } |
| 4191 | |
| 4192 | RValue<Float4> Floor(RValue<Float4> x) |
| 4193 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4194 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4195 | if(CPUID::SSE4_1) |
| 4196 | { |
| 4197 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4198 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4199 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4200 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4201 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4202 | round->addArg(::context->getConstantInt32(1)); |
| 4203 | ::basicBlock->appendInst(round); |
| 4204 | |
| 4205 | return RValue<Float4>(V(result)); |
| 4206 | } |
| 4207 | else |
| 4208 | { |
| 4209 | return x - Frac(x); |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | RValue<Float4> Ceil(RValue<Float4> x) |
| 4214 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4215 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4216 | if(CPUID::SSE4_1) |
| 4217 | { |
| 4218 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_v4f32); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4219 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Round, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4220 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4221 | auto round = Ice::InstIntrinsicCall::create(::function, 2, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4222 | round->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4223 | round->addArg(::context->getConstantInt32(2)); |
| 4224 | ::basicBlock->appendInst(round); |
| 4225 | |
| 4226 | return RValue<Float4>(V(result)); |
| 4227 | } |
| 4228 | else |
| 4229 | { |
| 4230 | return -Floor(-x); |
| 4231 | } |
| 4232 | } |
| 4233 | |
Nicolas Capens | 519cf22 | 2020-05-08 15:27:19 -0400 | [diff] [blame] | 4234 | Type *Float4::type() |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4235 | { |
| 4236 | return T(Ice::IceType_v4f32); |
| 4237 | } |
| 4238 | |
| 4239 | RValue<Long> Ticks() |
| 4240 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4241 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4242 | UNIMPLEMENTED_NO_BUG("RValue<Long> Ticks()"); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4243 | return Long(Int(0)); |
| 4244 | } |
| 4245 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4246 | RValue<Pointer<Byte>> ConstantPointer(void const *ptr) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4247 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4248 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4249 | return RValue<Pointer<Byte>>{ V(sz::getConstantPointer(::context, ptr)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4250 | } |
| 4251 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4252 | RValue<Pointer<Byte>> ConstantData(void const *data, size_t size) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4253 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4254 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 02a3953 | 2020-01-21 15:15:34 -0500 | [diff] [blame] | 4255 | return RValue<Pointer<Byte>>{ V(IceConstantData(data, size)) }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4256 | } |
| 4257 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4258 | Value *Call(RValue<Pointer<Byte>> fptr, Type *retTy, std::initializer_list<Value *> args, std::initializer_list<Type *> argTys) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4259 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4260 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4261 | return V(sz::Call(::function, ::basicBlock, T(retTy), V(fptr.value()), V(args), false)); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | void Breakpoint() |
| 4265 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4266 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4267 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Trap, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4268 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4269 | auto trap = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4270 | ::basicBlock->appendInst(trap); |
| 4271 | } |
| 4272 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4273 | void Nucleus::createFence(std::memory_order memoryOrder) |
| 4274 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4275 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4276 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::AtomicFence, Ice::Intrinsics::SideEffects_T, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4277 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4278 | auto inst = Ice::InstIntrinsicCall::create(::function, 0, nullptr, target, intrinsic); |
| 4279 | auto order = ::context->getConstantInt32(stdToIceMemoryOrder(memoryOrder)); |
| 4280 | inst->addArg(order); |
| 4281 | ::basicBlock->appendInst(inst); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4282 | } |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4283 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4284 | Value *Nucleus::createMaskedLoad(Value *ptr, Type *elTy, Value *mask, unsigned int alignment, bool zeroMaskedLanes) |
| 4285 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4286 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4287 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedLoad()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4288 | return nullptr; |
| 4289 | } |
| 4290 | void Nucleus::createMaskedStore(Value *ptr, Value *val, Value *mask, unsigned int alignment) |
| 4291 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4292 | RR_DEBUG_INFO_UPDATE_LOC(); |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4293 | UNIMPLEMENTED_NO_BUG("Subzero createMaskedStore()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4294 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4295 | |
| 4296 | RValue<Float4> Gather(RValue<Pointer<Float>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4297 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4298 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4299 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4300 | } |
| 4301 | |
| 4302 | RValue<Int4> Gather(RValue<Pointer<Int>> base, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment, bool zeroMaskedLanes /* = false */) |
| 4303 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4304 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4305 | return emulated::Gather(base, offsets, mask, alignment, zeroMaskedLanes); |
| 4306 | } |
| 4307 | |
| 4308 | void Scatter(RValue<Pointer<Float>> base, RValue<Float4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4309 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4310 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4311 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4312 | } |
| 4313 | |
| 4314 | void Scatter(RValue<Pointer<Int>> base, RValue<Int4> val, RValue<Int4> offsets, RValue<Int4> mask, unsigned int alignment) |
| 4315 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4316 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4317 | return emulated::Scatter(base, val, offsets, mask, alignment); |
| 4318 | } |
| 4319 | |
| 4320 | RValue<Float> Exp2(RValue<Float> x) |
| 4321 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4322 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4323 | return emulated::Exp2(x); |
| 4324 | } |
| 4325 | |
| 4326 | RValue<Float> Log2(RValue<Float> x) |
| 4327 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4328 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4329 | return emulated::Log2(x); |
| 4330 | } |
| 4331 | |
| 4332 | RValue<Float4> Sin(RValue<Float4> x) |
| 4333 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4334 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4335 | return optimal::Sin(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | RValue<Float4> Cos(RValue<Float4> x) |
| 4339 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4340 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4341 | return optimal::Cos(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | RValue<Float4> Tan(RValue<Float4> x) |
| 4345 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4346 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4347 | return optimal::Tan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4348 | } |
| 4349 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4350 | RValue<Float4> Asin(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4351 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4352 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4353 | if(p == Precision::Full) |
| 4354 | { |
| 4355 | return emulated::Asin(x); |
| 4356 | } |
| 4357 | return optimal::Asin_8_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4358 | } |
| 4359 | |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4360 | RValue<Float4> Acos(RValue<Float4> x, Precision p) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4361 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4362 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4363 | // Surprisingly, deqp-vk's precision.acos.highp/mediump tests pass when using the 4-term polynomial approximation |
| 4364 | // version of acos, unlike for Asin, which requires higher precision algorithms. |
| 4365 | return optimal::Acos_4_terms(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | RValue<Float4> Atan(RValue<Float4> x) |
| 4369 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4370 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4371 | return optimal::Atan(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | RValue<Float4> Sinh(RValue<Float4> x) |
| 4375 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4376 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4377 | return optimal::Sinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | RValue<Float4> Cosh(RValue<Float4> x) |
| 4381 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4382 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4383 | return optimal::Cosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4384 | } |
| 4385 | |
| 4386 | RValue<Float4> Tanh(RValue<Float4> x) |
| 4387 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4388 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4389 | return optimal::Tanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | RValue<Float4> Asinh(RValue<Float4> x) |
| 4393 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4394 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4395 | return optimal::Asinh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | RValue<Float4> Acosh(RValue<Float4> x) |
| 4399 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4400 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4401 | return optimal::Acosh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4402 | } |
| 4403 | |
| 4404 | RValue<Float4> Atanh(RValue<Float4> x) |
| 4405 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4406 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4407 | return optimal::Atanh(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | RValue<Float4> Atan2(RValue<Float4> x, RValue<Float4> y) |
| 4411 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4412 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4413 | return optimal::Atan2(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | RValue<Float4> Pow(RValue<Float4> x, RValue<Float4> y) |
| 4417 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4418 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4419 | return optimal::Pow(x, y); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | RValue<Float4> Exp(RValue<Float4> x) |
| 4423 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4424 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4425 | return optimal::Exp(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | RValue<Float4> Log(RValue<Float4> x) |
| 4429 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4430 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4431 | return optimal::Log(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | RValue<Float4> Exp2(RValue<Float4> x) |
| 4435 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4436 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4437 | return optimal::Exp2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | RValue<Float4> Log2(RValue<Float4> x) |
| 4441 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4442 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 9c14bda | 2020-09-18 16:33:36 -0400 | [diff] [blame] | 4443 | return optimal::Log2(x); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4444 | } |
| 4445 | |
| 4446 | RValue<UInt> Ctlz(RValue<UInt> x, bool isZeroUndef) |
| 4447 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4448 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4449 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4450 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4451 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4452 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4453 | } |
| 4454 | else |
| 4455 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4456 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4457 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Ctlz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4458 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4459 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4460 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4461 | ::basicBlock->appendInst(ctlz); |
| 4462 | |
| 4463 | return RValue<UInt>(V(result)); |
| 4464 | } |
| 4465 | } |
| 4466 | |
| 4467 | RValue<UInt4> Ctlz(RValue<UInt4> x, bool isZeroUndef) |
| 4468 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4469 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4470 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4471 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4472 | UNIMPLEMENTED_NO_BUG("Subzero Ctlz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4473 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4474 | } |
| 4475 | else |
| 4476 | { |
| 4477 | // TODO: implement vectorized version in Subzero |
| 4478 | UInt4 result; |
| 4479 | result = Insert(result, Ctlz(Extract(x, 0), isZeroUndef), 0); |
| 4480 | result = Insert(result, Ctlz(Extract(x, 1), isZeroUndef), 1); |
| 4481 | result = Insert(result, Ctlz(Extract(x, 2), isZeroUndef), 2); |
| 4482 | result = Insert(result, Ctlz(Extract(x, 3), isZeroUndef), 3); |
| 4483 | return result; |
| 4484 | } |
| 4485 | } |
| 4486 | |
| 4487 | RValue<UInt> Cttz(RValue<UInt> x, bool isZeroUndef) |
| 4488 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4489 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4490 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4491 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4492 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4493 | return UInt(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4494 | } |
| 4495 | else |
| 4496 | { |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4497 | Ice::Variable *result = ::function->makeVariable(Ice::IceType_i32); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4498 | const Ice::Intrinsics::IntrinsicInfo intrinsic = { Ice::Intrinsics::Cttz, Ice::Intrinsics::SideEffects_F, Ice::Intrinsics::ReturnsTwice_F, Ice::Intrinsics::MemoryWrite_F }; |
| 4499 | auto target = ::context->getConstantUndef(Ice::IceType_i32); |
| 4500 | auto ctlz = Ice::InstIntrinsicCall::create(::function, 1, result, target, intrinsic); |
Nicolas Capens | b6e8c3f | 2020-05-01 23:28:37 -0400 | [diff] [blame] | 4501 | ctlz->addArg(x.value()); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4502 | ::basicBlock->appendInst(ctlz); |
| 4503 | |
| 4504 | return RValue<UInt>(V(result)); |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | RValue<UInt4> Cttz(RValue<UInt4> x, bool isZeroUndef) |
| 4509 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4510 | RR_DEBUG_INFO_UPDATE_LOC(); |
Nicolas Capens | 81bc9d9 | 2019-12-16 15:05:57 -0500 | [diff] [blame] | 4511 | if(emulateIntrinsics) |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4512 | { |
Ben Clayton | ce54c59 | 2020-02-07 11:30:51 +0000 | [diff] [blame] | 4513 | UNIMPLEMENTED_NO_BUG("Subzero Cttz()"); |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4514 | return UInt4(0); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4515 | } |
| 4516 | else |
| 4517 | { |
| 4518 | // TODO: implement vectorized version in Subzero |
| 4519 | UInt4 result; |
| 4520 | result = Insert(result, Cttz(Extract(x, 0), isZeroUndef), 0); |
| 4521 | result = Insert(result, Cttz(Extract(x, 1), isZeroUndef), 1); |
| 4522 | result = Insert(result, Cttz(Extract(x, 2), isZeroUndef), 2); |
| 4523 | result = Insert(result, Cttz(Extract(x, 3), isZeroUndef), 3); |
| 4524 | return result; |
| 4525 | } |
| 4526 | } |
| 4527 | |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4528 | RValue<Int> MinAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4529 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4530 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4531 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4532 | } |
| 4533 | |
| 4534 | RValue<UInt> MinAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4535 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4536 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4537 | return emulated::MinAtomic(x, y, memoryOrder); |
| 4538 | } |
| 4539 | |
| 4540 | RValue<Int> MaxAtomic(RValue<Pointer<Int>> x, RValue<Int> y, std::memory_order memoryOrder) |
| 4541 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4542 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4543 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4544 | } |
| 4545 | |
| 4546 | RValue<UInt> MaxAtomic(RValue<Pointer<UInt>> x, RValue<UInt> y, std::memory_order memoryOrder) |
| 4547 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4548 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 370cba5 | 2019-12-31 11:36:07 -0500 | [diff] [blame] | 4549 | return emulated::MaxAtomic(x, y, memoryOrder); |
| 4550 | } |
| 4551 | |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4552 | void EmitDebugLocation() |
| 4553 | { |
| 4554 | #ifdef ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4555 | emitPrintLocation(getCallerBacktrace()); |
Antonio Maiorano | 4b77777 | 2020-06-22 14:55:37 -0400 | [diff] [blame] | 4556 | #endif // ENABLE_RR_DEBUG_INFO |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4557 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4558 | void EmitDebugVariable(Value *value) {} |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4559 | void FlushDebug() {} |
| 4560 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4561 | namespace { |
| 4562 | namespace coro { |
| 4563 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4564 | // Instance data per generated coroutine |
| 4565 | // This is the "handle" type used for Coroutine functions |
| 4566 | // Lifetime: from yield to when CoroutineEntryDestroy generated function is called. |
| 4567 | struct CoroutineData |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4568 | { |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4569 | bool useInternalScheduler = false; |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4570 | bool done = false; // the coroutine should stop at the next yield() |
| 4571 | bool terminated = false; // the coroutine has finished. |
| 4572 | bool inRoutine = false; // is the coroutine currently executing? |
| 4573 | marl::Scheduler::Fiber *mainFiber = nullptr; |
| 4574 | marl::Scheduler::Fiber *routineFiber = nullptr; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4575 | void *promisePtr = nullptr; |
| 4576 | }; |
| 4577 | |
| 4578 | CoroutineData *createCoroutineData() |
| 4579 | { |
| 4580 | return new CoroutineData{}; |
| 4581 | } |
| 4582 | |
| 4583 | void destroyCoroutineData(CoroutineData *coroData) |
| 4584 | { |
| 4585 | delete coroData; |
| 4586 | } |
| 4587 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4588 | // suspend() pauses execution of the coroutine, and resumes execution from the |
| 4589 | // caller's call to await(). |
| 4590 | // Returns true if await() is called again, or false if coroutine_destroy() |
| 4591 | // is called. |
| 4592 | bool suspend(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4593 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4594 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4595 | ASSERT(marl::Scheduler::Fiber::current() == coroData->routineFiber); |
| 4596 | ASSERT(coroData->inRoutine); |
| 4597 | coroData->inRoutine = false; |
| 4598 | coroData->mainFiber->notify(); |
| 4599 | while(!coroData->inRoutine) |
| 4600 | { |
| 4601 | coroData->routineFiber->wait(); |
| 4602 | } |
| 4603 | return !coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4604 | } |
| 4605 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4606 | // resume() is called by await(), blocking until the coroutine calls yield() |
| 4607 | // or the coroutine terminates. |
| 4608 | void resume(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4609 | { |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4610 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4611 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4612 | ASSERT(!coroData->inRoutine); |
| 4613 | coroData->inRoutine = true; |
| 4614 | coroData->routineFiber->notify(); |
| 4615 | while(coroData->inRoutine) |
| 4616 | { |
| 4617 | coroData->mainFiber->wait(); |
| 4618 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4619 | } |
| 4620 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4621 | // stop() is called by coroutine_destroy(), signalling that it's done, then blocks |
| 4622 | // until the coroutine ends, and deletes the coroutine data. |
| 4623 | void stop(Nucleus::CoroutineHandle handle) |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4624 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4625 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4626 | ASSERT(marl::Scheduler::Fiber::current() == coroData->mainFiber); |
| 4627 | ASSERT(!coroData->inRoutine); |
| 4628 | if(!coroData->terminated) |
| 4629 | { |
| 4630 | coroData->done = true; |
| 4631 | coroData->inRoutine = true; |
| 4632 | coroData->routineFiber->notify(); |
| 4633 | while(!coroData->terminated) |
| 4634 | { |
| 4635 | coroData->mainFiber->wait(); |
| 4636 | } |
| 4637 | } |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4638 | if(coroData->useInternalScheduler) |
| 4639 | { |
| 4640 | ::getOrCreateScheduler().unbind(); |
| 4641 | } |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4642 | coro::destroyCoroutineData(coroData); // free the coroutine data. |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | namespace detail { |
| 4646 | thread_local rr::Nucleus::CoroutineHandle coroHandle{}; |
| 4647 | } // namespace detail |
| 4648 | |
| 4649 | void setHandleParam(Nucleus::CoroutineHandle handle) |
| 4650 | { |
| 4651 | ASSERT(!detail::coroHandle); |
| 4652 | detail::coroHandle = handle; |
| 4653 | } |
| 4654 | |
| 4655 | Nucleus::CoroutineHandle getHandleParam() |
| 4656 | { |
| 4657 | ASSERT(detail::coroHandle); |
| 4658 | auto handle = detail::coroHandle; |
| 4659 | detail::coroHandle = {}; |
| 4660 | return handle; |
| 4661 | } |
| 4662 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4663 | bool isDone(Nucleus::CoroutineHandle handle) |
| 4664 | { |
| 4665 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4666 | return coroData->done; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | void setPromisePtr(Nucleus::CoroutineHandle handle, void *promisePtr) |
| 4670 | { |
| 4671 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4672 | coroData->promisePtr = promisePtr; |
| 4673 | } |
| 4674 | |
| 4675 | void *getPromisePtr(Nucleus::CoroutineHandle handle) |
| 4676 | { |
| 4677 | auto *coroData = reinterpret_cast<CoroutineData *>(handle); |
| 4678 | return coroData->promisePtr; |
| 4679 | } |
| 4680 | |
| 4681 | } // namespace coro |
| 4682 | } // namespace |
| 4683 | |
| 4684 | // Used to generate coroutines. |
| 4685 | // Lifetime: from yield to acquireCoroutine |
| 4686 | class CoroutineGenerator |
| 4687 | { |
| 4688 | public: |
| 4689 | CoroutineGenerator() |
| 4690 | { |
| 4691 | } |
| 4692 | |
| 4693 | // Inserts instructions at the top of the current function to make it a coroutine. |
| 4694 | void generateCoroutineBegin() |
| 4695 | { |
| 4696 | // Begin building the main coroutine_begin() function. |
| 4697 | // We insert these instructions at the top of the entry node, |
| 4698 | // before existing reactor-generated instructions. |
| 4699 | |
| 4700 | // CoroutineHandle coroutine_begin(<Arguments>) |
| 4701 | // { |
| 4702 | // this->handle = coro::getHandleParam(); |
| 4703 | // |
| 4704 | // YieldType promise; |
| 4705 | // coro::setPromisePtr(handle, &promise); // For await |
| 4706 | // |
| 4707 | // ... <REACTOR CODE> ... |
| 4708 | // |
| 4709 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4710 | // this->handle = coro::getHandleParam(); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4711 | this->handle = sz::Call(::function, ::entryBlock, coro::getHandleParam); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4712 | |
| 4713 | // YieldType promise; |
| 4714 | // coro::setPromisePtr(handle, &promise); // For await |
| 4715 | this->promise = sz::allocateStackVariable(::function, T(::coroYieldType)); |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4716 | sz::Call(::function, ::entryBlock, coro::setPromisePtr, this->handle, this->promise); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | // Adds instructions for Yield() calls at the current location of the main coroutine function. |
| 4720 | void generateYield(Value *val) |
| 4721 | { |
| 4722 | // ... <REACTOR CODE> ... |
| 4723 | // |
| 4724 | // promise = val; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4725 | // if (!coro::suspend(handle)) { |
| 4726 | // return false; // coroutine has been stopped by the caller. |
| 4727 | // } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4728 | // |
| 4729 | // ... <REACTOR CODE> ... |
| 4730 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4731 | // promise = val; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4732 | Nucleus::createStore(val, V(this->promise), ::coroYieldType); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4733 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4734 | // if (!coro::suspend(handle)) { |
| 4735 | auto result = sz::Call(::function, ::basicBlock, coro::suspend, this->handle); |
| 4736 | auto doneBlock = Nucleus::createBasicBlock(); |
| 4737 | auto resumeBlock = Nucleus::createBasicBlock(); |
| 4738 | Nucleus::createCondBr(V(result), resumeBlock, doneBlock); |
| 4739 | |
| 4740 | // return false; // coroutine has been stopped by the caller. |
| 4741 | ::basicBlock = doneBlock; |
| 4742 | Nucleus::createRetVoid(); // coroutine return value is ignored. |
| 4743 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4744 | // ... <REACTOR CODE> ... |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4745 | ::basicBlock = resumeBlock; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | using FunctionUniquePtr = std::unique_ptr<Ice::Cfg>; |
| 4749 | |
| 4750 | // Generates the await function for the current coroutine. |
| 4751 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4752 | static FunctionUniquePtr generateAwaitFunction() |
| 4753 | { |
| 4754 | // bool coroutine_await(CoroutineHandle handle, YieldType* out) |
| 4755 | // { |
| 4756 | // if (coro::isDone()) |
| 4757 | // { |
| 4758 | // return false; |
| 4759 | // } |
| 4760 | // else // resume |
| 4761 | // { |
| 4762 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4763 | // *out = *promise; |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4764 | // coro::resume(handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4765 | // return true; |
| 4766 | // } |
| 4767 | // } |
| 4768 | |
| 4769 | // Subzero doesn't support bool types (IceType_i1) as return type |
| 4770 | const Ice::Type ReturnType = Ice::IceType_i32; |
| 4771 | const Ice::Type YieldPtrType = sz::getPointerType(T(::coroYieldType)); |
| 4772 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4773 | |
| 4774 | Ice::Cfg *awaitFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType, YieldPtrType }); |
| 4775 | Ice::CfgLocalAllocatorScope scopedAlloc{ awaitFunc }; |
| 4776 | |
| 4777 | Ice::Variable *handle = awaitFunc->getArgs()[0]; |
| 4778 | Ice::Variable *outPtr = awaitFunc->getArgs()[1]; |
| 4779 | |
| 4780 | auto doneBlock = awaitFunc->makeNode(); |
| 4781 | { |
| 4782 | // return false; |
| 4783 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(0)); |
| 4784 | doneBlock->appendInst(ret); |
| 4785 | } |
| 4786 | |
| 4787 | auto resumeBlock = awaitFunc->makeNode(); |
| 4788 | { |
| 4789 | // YieldType* promise = coro::getPromisePtr(handle); |
| 4790 | Ice::Variable *promise = sz::Call(awaitFunc, resumeBlock, coro::getPromisePtr, handle); |
| 4791 | |
| 4792 | // *out = *promise; |
| 4793 | // Load promise value |
| 4794 | Ice::Variable *promiseVal = awaitFunc->makeVariable(T(::coroYieldType)); |
| 4795 | auto load = Ice::InstLoad::create(awaitFunc, promiseVal, promise); |
| 4796 | resumeBlock->appendInst(load); |
| 4797 | // Then store it in output param |
| 4798 | auto store = Ice::InstStore::create(awaitFunc, promiseVal, outPtr); |
| 4799 | resumeBlock->appendInst(store); |
| 4800 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4801 | // coro::resume(handle); |
| 4802 | sz::Call(awaitFunc, resumeBlock, coro::resume, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4803 | |
| 4804 | // return true; |
| 4805 | Ice::InstRet *ret = Ice::InstRet::create(awaitFunc, ::context->getConstantInt32(1)); |
| 4806 | resumeBlock->appendInst(ret); |
| 4807 | } |
| 4808 | |
| 4809 | // if (coro::isDone()) |
| 4810 | // { |
| 4811 | // <doneBlock> |
| 4812 | // } |
| 4813 | // else // resume |
| 4814 | // { |
| 4815 | // <resumeBlock> |
| 4816 | // } |
| 4817 | Ice::CfgNode *bb = awaitFunc->getEntryNode(); |
Antonio Maiorano | bc98fbe | 2020-03-17 15:46:22 -0400 | [diff] [blame] | 4818 | Ice::Variable *done = sz::Call(awaitFunc, bb, coro::isDone, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4819 | auto br = Ice::InstBr::create(awaitFunc, done, doneBlock, resumeBlock); |
| 4820 | bb->appendInst(br); |
| 4821 | |
| 4822 | return FunctionUniquePtr{ awaitFunc }; |
| 4823 | } |
| 4824 | |
| 4825 | // Generates the destroy function for the current coroutine. |
| 4826 | // Cannot use Nucleus functions that modify ::function and ::basicBlock. |
| 4827 | static FunctionUniquePtr generateDestroyFunction() |
| 4828 | { |
| 4829 | // void coroutine_destroy(Nucleus::CoroutineHandle handle) |
| 4830 | // { |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4831 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4832 | // return; |
| 4833 | // } |
| 4834 | |
| 4835 | const Ice::Type ReturnType = Ice::IceType_void; |
| 4836 | const Ice::Type HandleType = sz::getPointerType(Ice::IceType_void); |
| 4837 | |
| 4838 | Ice::Cfg *destroyFunc = sz::createFunction(::context, ReturnType, std::vector<Ice::Type>{ HandleType }); |
| 4839 | Ice::CfgLocalAllocatorScope scopedAlloc{ destroyFunc }; |
| 4840 | |
| 4841 | Ice::Variable *handle = destroyFunc->getArgs()[0]; |
| 4842 | |
| 4843 | auto *bb = destroyFunc->getEntryNode(); |
| 4844 | |
Antonio Maiorano | 8bce067 | 2020-02-28 13:13:45 -0500 | [diff] [blame] | 4845 | // coro::stop(handle); // signal and wait for coroutine to stop, and delete coroutine data |
| 4846 | sz::Call(destroyFunc, bb, coro::stop, handle); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4847 | |
| 4848 | // return; |
| 4849 | Ice::InstRet *ret = Ice::InstRet::create(destroyFunc); |
| 4850 | bb->appendInst(ret); |
| 4851 | |
| 4852 | return FunctionUniquePtr{ destroyFunc }; |
| 4853 | } |
| 4854 | |
| 4855 | private: |
| 4856 | Ice::Variable *handle{}; |
| 4857 | Ice::Variable *promise{}; |
| 4858 | }; |
| 4859 | |
| 4860 | static Nucleus::CoroutineHandle invokeCoroutineBegin(std::function<Nucleus::CoroutineHandle()> beginFunc) |
| 4861 | { |
| 4862 | // This doubles up as our coroutine handle |
| 4863 | auto coroData = coro::createCoroutineData(); |
| 4864 | |
Antonio Maiorano | 8f2d48f | 2020-02-28 13:39:11 -0500 | [diff] [blame] | 4865 | coroData->useInternalScheduler = (marl::Scheduler::get() == nullptr); |
| 4866 | if(coroData->useInternalScheduler) |
| 4867 | { |
| 4868 | ::getOrCreateScheduler().bind(); |
| 4869 | } |
| 4870 | |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4871 | auto run = [=] { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4872 | // Store handle in TLS so that the coroutine can grab it right away, before |
| 4873 | // any fiber switch occurs. |
| 4874 | coro::setHandleParam(coroData); |
| 4875 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4876 | ASSERT(!coroData->routineFiber); |
| 4877 | coroData->routineFiber = marl::Scheduler::Fiber::current(); |
| 4878 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4879 | beginFunc(); |
| 4880 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4881 | ASSERT(coroData->inRoutine); |
| 4882 | coroData->done = true; // coroutine is done. |
| 4883 | coroData->terminated = true; // signal that the coroutine data is ready for freeing. |
| 4884 | coroData->inRoutine = false; |
| 4885 | coroData->mainFiber->notify(); |
Ben Clayton | 76e9e53 | 2020-03-16 20:35:04 +0000 | [diff] [blame] | 4886 | }; |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4887 | |
Ben Clayton | c346653 | 2020-03-24 11:54:05 +0000 | [diff] [blame] | 4888 | ASSERT(!coroData->mainFiber); |
| 4889 | coroData->mainFiber = marl::Scheduler::Fiber::current(); |
| 4890 | |
| 4891 | // block until the first yield or coroutine end |
| 4892 | ASSERT(!coroData->inRoutine); |
| 4893 | coroData->inRoutine = true; |
| 4894 | marl::schedule(marl::Task(run, marl::Task::Flags::SameThread)); |
| 4895 | while(coroData->inRoutine) |
| 4896 | { |
| 4897 | coroData->mainFiber->wait(); |
| 4898 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4899 | |
| 4900 | return coroData; |
| 4901 | } |
| 4902 | |
| 4903 | void Nucleus::createCoroutine(Type *yieldType, const std::vector<Type *> ¶ms) |
| 4904 | { |
| 4905 | // Start by creating a regular function |
| 4906 | createFunction(yieldType, params); |
| 4907 | |
| 4908 | // Save in case yield() is called |
| 4909 | ASSERT(::coroYieldType == nullptr); // Only one coroutine can be generated at once |
| 4910 | ::coroYieldType = yieldType; |
| 4911 | } |
| 4912 | |
| 4913 | void Nucleus::yield(Value *val) |
| 4914 | { |
Antonio Maiorano | aae3373 | 2020-02-14 14:52:34 -0500 | [diff] [blame] | 4915 | RR_DEBUG_INFO_UPDATE_LOC(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4916 | Variable::materializeAll(); |
| 4917 | |
| 4918 | // On first yield, we start generating coroutine functions |
| 4919 | if(!::coroGen) |
| 4920 | { |
| 4921 | ::coroGen = std::make_shared<CoroutineGenerator>(); |
| 4922 | ::coroGen->generateCoroutineBegin(); |
| 4923 | } |
| 4924 | |
| 4925 | ASSERT(::coroGen); |
| 4926 | ::coroGen->generateYield(val); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4927 | } |
| 4928 | |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4929 | static bool coroutineEntryAwaitStub(Nucleus::CoroutineHandle, void *yieldValue) |
| 4930 | { |
| 4931 | return false; |
| 4932 | } |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4933 | |
| 4934 | static void coroutineEntryDestroyStub(Nucleus::CoroutineHandle handle) |
| 4935 | { |
| 4936 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4937 | |
| 4938 | std::shared_ptr<Routine> Nucleus::acquireCoroutine(const char *name, const Config::Edit &cfgEdit /* = Config::Edit::None */) |
| 4939 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4940 | if(::coroGen) |
| 4941 | { |
| 4942 | // Finish generating coroutine functions |
| 4943 | { |
| 4944 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4945 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4946 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4947 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4948 | auto awaitFunc = ::coroGen->generateAwaitFunction(); |
| 4949 | auto destroyFunc = ::coroGen->generateDestroyFunction(); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4950 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4951 | // At this point, we no longer need the CoroutineGenerator. |
| 4952 | ::coroGen.reset(); |
| 4953 | ::coroYieldType = nullptr; |
| 4954 | |
| 4955 | auto routine = rr::acquireRoutine({ ::function, awaitFunc.get(), destroyFunc.get() }, |
| 4956 | { name, "await", "destroy" }, |
| 4957 | cfgEdit); |
| 4958 | |
| 4959 | return routine; |
| 4960 | } |
| 4961 | else |
| 4962 | { |
| 4963 | { |
| 4964 | Ice::CfgLocalAllocatorScope scopedAlloc{ ::function }; |
Antonio Maiorano | 22d73d1 | 2020-03-20 00:13:28 -0400 | [diff] [blame] | 4965 | finalizeFunction(); |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4966 | } |
| 4967 | |
| 4968 | ::coroYieldType = nullptr; |
| 4969 | |
| 4970 | // Not an actual coroutine (no yields), so return stubs for await and destroy |
| 4971 | auto routine = rr::acquireRoutine({ ::function }, { name }, cfgEdit); |
| 4972 | |
| 4973 | auto routineImpl = std::static_pointer_cast<ELFMemoryStreamer>(routine); |
| 4974 | routineImpl->setEntry(Nucleus::CoroutineEntryAwait, reinterpret_cast<const void *>(&coroutineEntryAwaitStub)); |
| 4975 | routineImpl->setEntry(Nucleus::CoroutineEntryDestroy, reinterpret_cast<const void *>(&coroutineEntryDestroyStub)); |
| 4976 | return routine; |
| 4977 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4978 | } |
| 4979 | |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4980 | Nucleus::CoroutineHandle Nucleus::invokeCoroutineBegin(Routine &routine, std::function<Nucleus::CoroutineHandle()> func) |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4981 | { |
Antonio Maiorano | 5ba2a5b | 2020-01-17 15:29:37 -0500 | [diff] [blame] | 4982 | const bool isCoroutine = routine.getEntry(Nucleus::CoroutineEntryAwait) != reinterpret_cast<const void *>(&coroutineEntryAwaitStub); |
| 4983 | |
| 4984 | if(isCoroutine) |
| 4985 | { |
| 4986 | return rr::invokeCoroutineBegin(func); |
| 4987 | } |
| 4988 | else |
| 4989 | { |
| 4990 | // For regular routines, just invoke the begin func directly |
| 4991 | return func(); |
| 4992 | } |
Ben Clayton | 713b8d3 | 2019-12-17 20:37:56 +0000 | [diff] [blame] | 4993 | } |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 4994 | |
| 4995 | } // namespace rr |