gollvm: add support for __atomic_load_1 and __atomic_store_1 builtins

Add support for recognizing "__atomic_load_1" and "__atomic_store_1"
as compiler builtins. Needed for the Go 1.18 update.

Change-Id: I4f8b1860424619469a43aab4923c3c6c64ed17ca
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/385183
Trust: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/bridge/go-llvm-builtins.cpp b/bridge/go-llvm-builtins.cpp
index fcfea48..8e7224a 100644
--- a/bridge/go-llvm-builtins.cpp
+++ b/bridge/go-llvm-builtins.cpp
@@ -410,7 +410,13 @@
                                     Llvm_backend *be, int sz)
 {
   assert(args.size() == 2);
-  llvm::Type *t = sz == 8 ? be->llvmInt64Type() : be->llvmInt32Type();
+  llvm::Type *t = nullptr;
+  switch(sz) {
+    case 8: t = be->llvmInt64Type(); break;
+    case 4: t = be->llvmInt32Type(); break;
+    case 1: t = be->llvmInt8Type(); break;
+    default: llvm_unreachable("bad size passed to atomicLoadMaker");
+  }
   llvm::LoadInst *load = builder->CreateLoad(t, args[0]);
   // FIXME: we assume the FE always emits constant memory order.
   // in case it doesn't, conservatively use SequentiallyConsistent.
@@ -423,6 +429,13 @@
   return load;
 }
 
+static llvm::Value *atomicLoad1Maker(llvm::SmallVectorImpl<llvm::Value*> &args,
+                                     BlockLIRBuilder *builder,
+                                     Llvm_backend *be)
+{
+  return atomicLoadMaker(args, builder, be, 1);
+}
+
 static llvm::Value *atomicLoad4Maker(llvm::SmallVectorImpl<llvm::Value*> &args,
                                      BlockLIRBuilder *builder,
                                      Llvm_backend *be)
@@ -453,6 +466,13 @@
   return store;
 }
 
+static llvm::Value *atomicStore1Maker(llvm::SmallVectorImpl<llvm::Value*> &args,
+                                      BlockLIRBuilder *builder,
+                                      Llvm_backend *be)
+{
+  return atomicStoreMaker(args, builder, be, 1);
+}
+
 static llvm::Value *atomicStore4Maker(llvm::SmallVectorImpl<llvm::Value*> &args,
                                       BlockLIRBuilder *builder,
                                       Llvm_backend *be)
@@ -578,6 +598,11 @@
   }
 
   {
+    BuiltinEntryTypeVec typeVec = {uint8Type, uint8PtrType, int32Type};
+    registerExprBuiltin("__atomic_load_1", nullptr,
+                        typeVec, atomicLoad1Maker);
+  }
+  {
     BuiltinEntryTypeVec typeVec = {uint32Type, uint32PtrType, int32Type};
     registerExprBuiltin("__atomic_load_4", nullptr,
                         typeVec, atomicLoad4Maker);
@@ -589,6 +614,11 @@
   }
 
   {
+    BuiltinEntryTypeVec typeVec = {nullptr, uint8PtrType, uint8Type, int32Type};
+    registerExprBuiltin("__atomic_store_1", nullptr,
+                        typeVec, atomicStore1Maker);
+  }
+  {
     BuiltinEntryTypeVec typeVec = {nullptr, uint32PtrType, uint32Type, int32Type};
     registerExprBuiltin("__atomic_store_4", nullptr,
                         typeVec, atomicStore4Maker);