Fix windows build (#241)

* Fixing MSVC compile
* Formatted file
diff --git a/lib/SPIRVProducerPass.cpp b/lib/SPIRVProducerPass.cpp
index e7a0746..c0e00b8 100644
--- a/lib/SPIRVProducerPass.cpp
+++ b/lib/SPIRVProducerPass.cpp
@@ -28,7 +28,6 @@
 #include <unordered_set>
 #include <utility>
 
-
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/UniqueVector.h"
 #include "llvm/Analysis/LoopInfo.h"
@@ -77,7 +76,7 @@
 const double kOneOverPi = 0.318309886183790671538;
 const glsl::ExtInst kGlslExtInstBad = static_cast<glsl::ExtInst>(0);
 
-const char* kCompositeConstructFunctionPrefix = "clspv.composite_construct.";
+const char *kCompositeConstructFunctionPrefix = "clspv.composite_construct.";
 
 enum SPIRVOperandType {
   NUMBERID,
@@ -124,8 +123,8 @@
 class SPIRVOperandList {
 public:
   SPIRVOperandList() {}
-  SPIRVOperandList(const SPIRVOperandList& other) = delete;
-  SPIRVOperandList(SPIRVOperandList&& other) {
+  SPIRVOperandList(const SPIRVOperandList &other) = delete;
+  SPIRVOperandList(SPIRVOperandList &&other) {
     contents_ = std::move(other.contents_);
     other.contents_.clear();
   }
@@ -133,7 +132,7 @@
       : contents_(init.begin(), init.end()) {}
   operator ArrayRef<SPIRVOperand *>() { return contents_; }
   void push_back(SPIRVOperand *op) { contents_.push_back(op); }
-  void clear() { contents_.clear();}
+  void clear() { contents_.clear(); }
   size_t size() const { return contents_.size(); }
   SPIRVOperand *&operator[](size_t i) { return contents_[i]; }
 
@@ -142,7 +141,7 @@
   }
 
 private:
-  SmallVector<SPIRVOperand *,8> contents_;
+  SmallVector<SPIRVOperand *, 8> contents_;
 };
 
 SPIRVOperandList &operator<<(SPIRVOperandList &list, SPIRVOperand *elem) {
@@ -150,19 +149,17 @@
   return list;
 }
 
-SPIRVOperand* MkNum(uint32_t num) {
+SPIRVOperand *MkNum(uint32_t num) {
   return new SPIRVOperand(LITERAL_INTEGER, num);
 }
-SPIRVOperand* MkInteger(ArrayRef<uint32_t> num_vec) {
+SPIRVOperand *MkInteger(ArrayRef<uint32_t> num_vec) {
   return new SPIRVOperand(LITERAL_INTEGER, num_vec);
 }
-SPIRVOperand* MkFloat(ArrayRef<uint32_t> num_vec) {
+SPIRVOperand *MkFloat(ArrayRef<uint32_t> num_vec) {
   return new SPIRVOperand(LITERAL_FLOAT, num_vec);
 }
-SPIRVOperand* MkId(uint32_t id) {
-  return new SPIRVOperand(NUMBERID, id);
-}
-SPIRVOperand* MkString(StringRef str) {
+SPIRVOperand *MkId(uint32_t id) { return new SPIRVOperand(NUMBERID, id); }
+SPIRVOperand *MkString(StringRef str) {
   return new SPIRVOperand(LITERAL_STRING, str);
 }
 
@@ -228,8 +225,8 @@
         descriptorMapOut(descriptor_map_out), outputAsm(outputAsm),
         outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1),
         OpExtInstImportID(0), HasVariablePointers(false), SamplerTy(nullptr),
-        WorkgroupSizeValueID(0), WorkgroupSizeVarID(0),
-        max_local_spec_id_(0), constant_i32_zero_id_(0) {}
+        WorkgroupSizeValueID(0), WorkgroupSizeVarID(0), max_local_spec_id_(0),
+        constant_i32_zero_id_(0) {}
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.addRequired<DominatorTreeWrapperPass>();
@@ -277,23 +274,25 @@
   ValueList &getEntryPointInterfacesVec() { return EntryPointInterfacesVec; };
   uint32_t &getOpExtInstImportID() { return OpExtInstImportID; };
   std::vector<uint32_t> &getBuiltinDimVec() { return BuiltinDimensionVec; };
-  bool hasVariablePointers() { return true; /* We use StorageBuffer everywhere */ };
+  bool hasVariablePointers() {
+    return true; /* We use StorageBuffer everywhere */
+  };
   void setVariablePointers(bool Val) { HasVariablePointers = Val; };
-  ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() { return samplerMap; }
+  ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() {
+    return samplerMap;
+  }
   GlobalConstFuncMapType &getGlobalConstFuncTypeMap() {
     return GlobalConstFuncTypeMap;
   }
   SmallPtrSet<Value *, 16> &getGlobalConstArgSet() {
     return GlobalConstArgumentSet;
   }
-  TypeList &getTypesNeedingArrayStride() {
-    return TypesNeedingArrayStride;
-  }
+  TypeList &getTypesNeedingArrayStride() { return TypesNeedingArrayStride; }
 
   void GenerateLLVMIRInfo(Module &M, const DataLayout &DL);
-  // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will *not*
-  // be converted to a storage buffer, replace each such global variable with
-  // one in the storage class expecgted by SPIR-V.
+  // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will
+  // *not* be converted to a storage buffer, replace each such global variable
+  // with one in the storage class expecgted by SPIR-V.
   void FindGlobalConstVars(Module &M, const DataLayout &DL);
   // Populate ResourceVarInfoList, FunctionToResourceVarsMap, and
   // ModuleOrderedResourceVars.
@@ -304,8 +303,8 @@
   void FindTypePerFunc(Function &F);
   void FindTypesForSamplerMap(Module &M);
   void FindTypesForResourceVars(Module &M);
-  // Inserts |Ty| and relevant sub-types into the |Types| member, indicating that
-  // |Ty| and its subtypes will need a corresponding SPIR-V type.
+  // Inserts |Ty| and relevant sub-types into the |Types| member, indicating
+  // that |Ty| and its subtypes will need a corresponding SPIR-V type.
   void FindType(Type *Ty);
   void FindConstantPerGlobalVar(GlobalVariable &GV);
   void FindConstantPerFunc(Function &F);
@@ -316,14 +315,14 @@
   // allocated sequentially starting with the current value of nextID, and
   // with a type following its subtypes.  Also updates nextID to just beyond
   // the last generated ID.
-  void GenerateSPIRVTypes(LLVMContext& context, Module &module);
+  void GenerateSPIRVTypes(LLVMContext &context, Module &module);
   void GenerateSPIRVConstants();
   void GenerateModuleInfo(Module &M);
   void GenerateGlobalVar(GlobalVariable &GV);
   void GenerateWorkgroupVars();
   // Generate descriptor map entries for resource variables associated with
   // arguments to F.
-  void GenerateDescriptorMapInfo(const DataLayout& DL, Function& F);
+  void GenerateDescriptorMapInfo(const DataLayout &DL, Function &F);
   void GenerateSamplers(Module &M);
   // Generate OpVariables for %clspv.resource.var.* calls.
   void GenerateResourceVars(Module &M);
@@ -336,7 +335,7 @@
   void GenerateInstruction(Instruction &I);
   void GenerateFuncEpilogue();
   void HandleDeferredInstruction();
-  void HandleDeferredDecorations(const DataLayout& DL);
+  void HandleDeferredDecorations(const DataLayout &DL);
   bool is4xi8vec(Type *Ty) const;
   // Return the SPIR-V Id for 32-bit constant zero.  The constant must already
   // have been created.
@@ -383,7 +382,7 @@
   void WriteSPIRVBinary();
 
   // Returns true if |type| is compatible with OpConstantNull.
-  bool IsTypeNullable(const Type* type) const;
+  bool IsTypeNullable(const Type *type) const;
 
   // Populate UBO remapped type maps.
   void PopulateUBOTypeMaps(Module &module);
@@ -410,7 +409,7 @@
   // Binary output writes to this stream, which might be |out| or
   // |binaryTempOut|.  It's the latter when we really want to write a C
   // initializer list.
-  raw_pwrite_stream* binaryOut;
+  raw_pwrite_stream *binaryOut;
   raw_ostream &descriptorMapOut;
   const bool outputAsm;
   const bool outputCInitList; // If true, output look like {0x7023, ... , 5}
@@ -436,7 +435,7 @@
   std::vector<uint32_t> BuiltinDimensionVec;
   bool HasVariablePointers;
   Type *SamplerTy;
-  DenseMap<unsigned,uint32_t> SamplerMapIndexToIDMap;
+  DenseMap<unsigned, uint32_t> SamplerMapIndexToIDMap;
 
   // If a function F has a pointer-to-__constant parameter, then this variable
   // will map F's type to (G, index of the parameter), where in a first phase
@@ -487,7 +486,7 @@
   SmallVector<std::unique_ptr<ResourceVarInfo>, 8> ResourceVarInfoList;
   // This is a vector of pointers of all the resource vars, but ordered by
   // kernel function, and then by argument.
-  UniqueVector<ResourceVarInfo*> ModuleOrderedResourceVars;
+  UniqueVector<ResourceVarInfo *> ModuleOrderedResourceVars;
   // Map a function to the ordered list of resource variables it uses, one for
   // each argument.  If an argument does not use a resource variable, it
   // will have a null pointer entry.
@@ -507,14 +506,14 @@
   // One larger than the maximum used SpecId for pointer-to-local arguments.
   int max_local_spec_id_;
   // An ordered list of the kernel arguments of type pointer-to-local.
-  using LocalArgList = SmallVector<Argument*, 8>;
+  using LocalArgList = SmallVector<Argument *, 8>;
   LocalArgList LocalArgs;
   // Information about a pointer-to-local argument.
   struct LocalArgInfo {
     // The SPIR-V ID of the array variable.
     uint32_t variable_id;
     // The element type of the
-    Type* elem_type;
+    Type *elem_type;
     // The ID of the array type.
     uint32_t array_size_id;
     // The ID of the array type.
@@ -525,13 +524,14 @@
     int spec_id;
   };
   // A mapping from Argument to its assigned SpecId.
-  DenseMap<const Argument*, int> LocalArgSpecIds;
+  DenseMap<const Argument *, int> LocalArgSpecIds;
   // A mapping from SpecId to its LocalArgInfo.
   DenseMap<int, LocalArgInfo> LocalSpecIdInfoMap;
   // A mapping from a remapped type to its real offsets.
-  DenseMap<Type*, std::vector<uint32_t>> RemappedUBOTypeOffsets;
+  DenseMap<Type *, std::vector<uint32_t>> RemappedUBOTypeOffsets;
   // A mapping from a remapped type to its real sizes.
-  DenseMap<Type*, std::tuple<uint64_t, uint64_t, uint64_t>> RemappedUBOTypeSizes;
+  DenseMap<Type *, std::tuple<uint64_t, uint64_t, uint64_t>>
+      RemappedUBOTypeSizes;
 
   // The ID of 32-bit integer zero constant.  This is only valid after
   // GenerateSPIRVConstants has run.
@@ -540,7 +540,7 @@
 
 char SPIRVProducerPass::ID;
 
-}
+} // namespace
 
 namespace clspv {
 ModulePass *
@@ -703,9 +703,9 @@
     out << "; Schema: 0\n";
   } else {
     binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber),
-              sizeof(spv::MagicNumber));
+                     sizeof(spv::MagicNumber));
     binaryOut->write(reinterpret_cast<const char *>(&spv::Version),
-              sizeof(spv::Version));
+                     sizeof(spv::Version));
 
     // use Codeplay's vendor ID
     const uint32_t vendor = 3 << 16;
@@ -763,11 +763,11 @@
 
   //#error "remove arg handling from this code"
   // Map kernel functions to their ordinal number in the compilation unit.
-  UniqueVector<Function*> KernelOrdinal;
+  UniqueVector<Function *> KernelOrdinal;
 
   // Map the global variables created for kernel args to their creation
   // order.
-  UniqueVector<GlobalVariable*> KernelArgVarOrdinal;
+  UniqueVector<GlobalVariable *> KernelArgVarOrdinal;
 
   // For each kernel argument type, record the kernel arg global resource
   // variables generated for that type, the function in which that variable
@@ -1419,22 +1419,27 @@
   // module-level metadata. Translate that information into local argument
   // information.
   NamedMDNode *nmd = M.getNamedMetadata(clspv::LocalSpecIdMetadataName());
-  if (!nmd) return;
+  if (!nmd)
+    return;
   for (auto operand : nmd->operands()) {
     MDTuple *tuple = cast<MDTuple>(operand);
     ValueAsMetadata *fn_md = cast<ValueAsMetadata>(tuple->getOperand(0));
     Function *func = cast<Function>(fn_md->getValue());
-    ConstantAsMetadata *arg_index_md = cast<ConstantAsMetadata>(tuple->getOperand(1));
-    int arg_index = static_cast<int>(cast<ConstantInt>(arg_index_md->getValue())->getSExtValue());
-    Argument* arg = &*(func->arg_begin() + arg_index);
+    ConstantAsMetadata *arg_index_md =
+        cast<ConstantAsMetadata>(tuple->getOperand(1));
+    int arg_index = static_cast<int>(
+        cast<ConstantInt>(arg_index_md->getValue())->getSExtValue());
+    Argument *arg = &*(func->arg_begin() + arg_index);
 
     ConstantAsMetadata *spec_id_md =
         cast<ConstantAsMetadata>(tuple->getOperand(2));
-    int spec_id = static_cast<int>(cast<ConstantInt>(spec_id_md->getValue())->getSExtValue());
+    int spec_id = static_cast<int>(
+        cast<ConstantInt>(spec_id_md->getValue())->getSExtValue());
 
     max_local_spec_id_ = std::max(max_local_spec_id_, spec_id + 1);
     LocalArgSpecIds[arg] = spec_id;
-    if (LocalSpecIdInfoMap.count(spec_id)) continue;
+    if (LocalSpecIdInfoMap.count(spec_id))
+      continue;
 
     // We haven't seen this SpecId yet, so generate the LocalArgInfo for it.
     LocalArgInfo info{nextID,     arg->getType()->getPointerElementType(),
@@ -1623,15 +1628,20 @@
         }
 
         continue;
-      } else if ((Instruction::Xor == I.getOpcode()) && I.getType()->isIntegerTy(1)) {
-        // We special case for Xor where the type is i1 and one of the arguments is a constant 1 (true), this is an OpLogicalNot in SPIR-V, and we don't need the constant
+      } else if ((Instruction::Xor == I.getOpcode()) &&
+                 I.getType()->isIntegerTy(1)) {
+        // We special case for Xor where the type is i1 and one of the arguments
+        // is a constant 1 (true), this is an OpLogicalNot in SPIR-V, and we
+        // don't need the constant
         bool foundConstantTrue = false;
         for (Use &Op : I.operands()) {
           if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) {
             auto CI = cast<ConstantInt>(Op);
 
             if (CI->isZero() || foundConstantTrue) {
-              // If we already found the true constant, we might (probably only on -O0) have an OpLogicalNot which is taking a constant argument, so discover it anyway.
+              // If we already found the true constant, we might (probably only
+              // on -O0) have an OpLogicalNot which is taking a constant
+              // argument, so discover it anyway.
               FindConstant(Op);
             } else {
               foundConstantTrue = true;
@@ -1783,7 +1793,8 @@
                                                MkString("GLSL.std.450")));
 }
 
-void SPIRVProducerPass::GenerateSPIRVTypes(LLVMContext& Context, Module &module) {
+void SPIRVProducerPass::GenerateSPIRVTypes(LLVMContext &Context,
+                                           Module &module) {
   SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
   ValueMapType &VMap = getValueMap();
   ValueMapType &AllocatedVMap = getAllocatedValueMap();
@@ -1818,9 +1829,9 @@
       if (AddressSpace::Constant == AddrSpace) {
         if (!clspv::Option::ConstantArgsInUniformBuffer()) {
           AddrSpace = AddressSpace::Global;
-          // Check to see if we already created this type (for instance, if we had
-          // a constant <type>* and a global <type>*, the type would be created by
-          // one of these types, and shared by both).
+          // Check to see if we already created this type (for instance, if we
+          // had a constant <type>* and a global <type>*, the type would be
+          // created by one of these types, and shared by both).
           auto GlobalTy = PTy->getPointerElementType()->getPointerTo(AddrSpace);
           if (0 < TypeMap.count(GlobalTy)) {
             TypeMap[PTy] = TypeMap[GlobalTy];
@@ -1832,10 +1843,11 @@
         if (!clspv::Option::ConstantArgsInUniformBuffer()) {
           AddrSpace = AddressSpace::Constant;
 
-          // Check to see if we already created this type (for instance, if we had
-          // a constant <type>* and a global <type>*, the type would be created by
-          // one of these types, and shared by both).
-          auto ConstantTy = PTy->getPointerElementType()->getPointerTo(AddrSpace);
+          // Check to see if we already created this type (for instance, if we
+          // had a constant <type>* and a global <type>*, the type would be
+          // created by one of these types, and shared by both).
+          auto ConstantTy =
+              PTy->getPointerElementType()->getPointerTo(AddrSpace);
           if (0 < TypeMap.count(ConstantTy)) {
             TypeMap[PTy] = TypeMap[ConstantTy];
             UseExistingOpTypePointer = true;
@@ -1950,8 +1962,7 @@
 
       uint32_t STyID = nextID;
 
-      auto *Inst =
-          new SPIRVInstruction(spv::OpTypeStruct, nextID++, Ops);
+      auto *Inst = new SPIRVInstruction(spv::OpTypeStruct, nextID++, Ops);
       SPIRVInstList.push_back(Inst);
 
       // Generate OpMemberDecorate.
@@ -1982,11 +1993,12 @@
 
         Ops << MkId(STyID) << MkNum(MemberIdx) << MkNum(spv::DecorationOffset);
 
-        auto ByteOffset = StructLayout->getElementOffset(MemberIdx);
+        auto ByteOffset =
+            static_cast<uint32_t>(StructLayout->getElementOffset(MemberIdx));
         if (offsets) {
           ByteOffset = (*offsets)[MemberIdx];
         }
-        //const auto ByteOffset =
+        // const auto ByteOffset =
         //    uint32_t(StructLayout->getElementOffset(MemberIdx));
         Ops << MkNum(ByteOffset);
 
@@ -2024,7 +2036,7 @@
           aliasToWidth = 8;
         }
         if (aliasToWidth) {
-          Type* otherType = Type::getIntNTy(Ty->getContext(), aliasToWidth);
+          Type *otherType = Type::getIntNTy(Ty->getContext(), aliasToWidth);
           auto where = TypeMap.find(otherType);
           if (where == TypeMap.end()) {
             // Go ahead and make it, but also map the other type to it.
@@ -2173,7 +2185,8 @@
       Ops << MkId(lookupType(Ty->getVectorElementType()))
           << MkNum(Ty->getVectorNumElements());
 
-      SPIRVInstruction* inst = new SPIRVInstruction(spv::OpTypeVector, nextID++, Ops);
+      SPIRVInstruction *inst =
+          new SPIRVInstruction(spv::OpTypeVector, nextID++, Ops);
       SPIRVInstList.push_back(inst);
       break;
     }
@@ -2238,7 +2251,7 @@
   // Generate types for pointer-to-local arguments.
   for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_;
        ++spec_id) {
-    LocalArgInfo& arg_info = LocalSpecIdInfoMap[spec_id];
+    LocalArgInfo &arg_info = LocalSpecIdInfoMap[spec_id];
 
     // Generate the spec constant.
     SPIRVOperandList Ops;
@@ -2272,7 +2285,7 @@
 
   for (uint32_t i = 0; i < CstList.size(); i++) {
     // UniqueVector ids are 1-based.
-    Constant *Cst = cast<Constant>(CstList[i+1]);
+    Constant *Cst = cast<Constant>(CstList[i + 1]);
 
     // OpTypeArray's constant was already generated.
     if (AllocatedVMap.find_as(Cst) != AllocatedVMap.end()) {
@@ -2404,7 +2417,7 @@
         for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end();
              I != E; ++I) {
           uint64_t Val = 0;
-          const Value* CV = *I;
+          const Value *CV = *I;
           if (auto *CI2 = dyn_cast<ConstantInt>(CV)) {
             Val = CI2->getZExtValue();
           }
@@ -2455,7 +2468,7 @@
 void SPIRVProducerPass::GenerateSamplers(Module &M) {
   SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
 
-  auto& sampler_map = getSamplerMap();
+  auto &sampler_map = getSamplerMap();
   SamplerMapIndexToIDMap.clear();
   DenseMap<unsigned, unsigned> SamplerLiteralToIDMap;
   DenseMap<unsigned, unsigned> SamplerLiteralToDescriptorSetMap;
@@ -2466,8 +2479,9 @@
   // for them and bindings too.
   DenseSet<unsigned> used_bindings;
 
-  auto* var_fn = M.getFunction("clspv.sampler.var.literal");
-  if (!var_fn) return;
+  auto *var_fn = M.getFunction("clspv.sampler.var.literal");
+  if (!var_fn)
+    return;
   for (auto user : var_fn->users()) {
     // Populate SamplerLiteralToDescriptorSetMap and
     // SamplerLiteralToBindingMap.
@@ -2478,11 +2492,12 @@
     //          i32 descriptor,
     //          i32 binding,
     //          i32 index-into-sampler-map)
-    if (auto* call = dyn_cast<CallInst>(user)) {
-      const auto index_into_sampler_map =
-          dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue();
+    if (auto *call = dyn_cast<CallInst>(user)) {
+      const size_t index_into_sampler_map = static_cast<size_t>(
+          dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue());
       if (index_into_sampler_map >= sampler_map.size()) {
-        errs() << "Out of bounds index to sampler map: " << index_into_sampler_map;
+        errs() << "Out of bounds index to sampler map: "
+               << index_into_sampler_map;
         llvm_unreachable("bad sampler init: out of bounds");
       }
 
@@ -2532,7 +2547,8 @@
 
     unsigned descriptor_set;
     unsigned binding;
-    if(SamplerLiteralToBindingMap.find(SamplerLiteral.first) == SamplerLiteralToBindingMap.end()) {
+    if (SamplerLiteralToBindingMap.find(SamplerLiteral.first) ==
+        SamplerLiteralToBindingMap.end()) {
       // This sampler is not actually used.  Find the next one.
       for (binding = 0; used_bindings.count(binding); binding++)
         ;
@@ -2682,7 +2698,7 @@
 }
 
 void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) {
-  Module& M = *GV.getParent();
+  Module &M = *GV.getParent();
   SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
   ValueMapType &VMap = getValueMap();
   std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec();
@@ -2811,12 +2827,10 @@
       SPIRVInstList.push_back(
           new SPIRVInstruction(spv::OpSpecConstant, ZDimCstID, Ops));
 
-
       BuiltinDimVec.push_back(XDimCstID);
       BuiltinDimVec.push_back(YDimCstID);
       BuiltinDimVec.push_back(ZDimCstID);
 
-
       //
       // Generate OpSpecConstantComposite.
       //
@@ -2943,7 +2957,7 @@
   SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
   for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_;
        ++spec_id) {
-    LocalArgInfo& info = LocalSpecIdInfoMap[spec_id];
+    LocalArgInfo &info = LocalSpecIdInfoMap[spec_id];
 
     // Generate OpVariable.
     //
@@ -2987,8 +3001,8 @@
       const auto old_index =
           dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue();
       // Remapped argument index
-      const auto new_index =
-          dyn_extract<ConstantInt>(arg_node->getOperand(2))->getZExtValue();
+      const size_t new_index = static_cast<size_t>(
+          dyn_extract<ConstantInt>(arg_node->getOperand(2))->getZExtValue());
       const auto offset =
           dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue();
       const auto arg_size =
@@ -3005,7 +3019,8 @@
                          << argKind << ",arrayElemSize,"
                          << GetTypeAllocSize(
                                 func_ty->getParamType(unsigned(new_index))
-                                    ->getPointerElementType(), DL)
+                                    ->getPointerElementType(),
+                                DL)
                          << ",arrayNumElemSpecId," << spec_id << "\n";
       } else {
         auto *info = resource_var_at_index[new_index];
@@ -3034,16 +3049,16 @@
     for (auto *info : resource_var_at_index) {
       if (info) {
         auto arg = arguments[arg_index];
-        unsigned arg_size;
+        unsigned arg_size = 0;
         if (info->arg_kind == clspv::ArgKind::Pod) {
-          arg_size = DL.getTypeStoreSize(arg->getType());
+          arg_size = static_cast<uint32_t>(DL.getTypeStoreSize(arg->getType()));
         }
 
         descriptorMapOut << "kernel," << F.getName() << ",arg,"
-                         << arg->getName() << ",argOrdinal,"
-                         << arg_index << ",descriptorSet,"
-                         << info->descriptor_set << ",binding," << info->binding
-                         << ",offset," << 0 << ",argKind,"
+                         << arg->getName() << ",argOrdinal," << arg_index
+                         << ",descriptorSet," << info->descriptor_set
+                         << ",binding," << info->binding << ",offset," << 0
+                         << ",argKind,"
                          << remap_arg_kind(
                                 clspv::GetArgKindName(info->arg_kind));
         if (info->arg_kind == clspv::ArgKind::Pod) {
@@ -3180,7 +3195,7 @@
   }
 }
 
-void SPIRVProducerPass::GenerateModuleInfo(Module& module) {
+void SPIRVProducerPass::GenerateModuleInfo(Module &module) {
   SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
   EntryPointVecType &EntryPoints = getEntryPointVec();
   ValueMapType &VMap = getValueMap();
@@ -3318,7 +3333,7 @@
     //
     // TODO: Do we need to consider Interface ID for forward references???
     Ops.clear();
-    const StringRef& name = EntryPoint.first->getName();
+    const StringRef &name = EntryPoint.first->getName();
     Ops << MkNum(spv::ExecutionModelGLCompute) << MkId(EntryPoint.second)
         << MkString(name);
 
@@ -3347,8 +3362,7 @@
       // Ops[1] = Execution Mode
       // Ops[2] ... Ops[n] = Optional literals according to Execution Mode
       Ops.clear();
-      Ops << MkId(EntryPoint.second)
-          << MkNum(spv::ExecutionModeLocalSize);
+      Ops << MkId(EntryPoint.second) << MkNum(spv::ExecutionModeLocalSize);
 
       uint32_t XDim = static_cast<uint32_t>(
           mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue());
@@ -3410,7 +3424,8 @@
   // variables.  Yes, *every* entry point pays this cost if *any* entry point
   // uses this builtin.  At this point I judge this to be an acceptable tradeoff
   // of complexity vs. runtime, for a broken driver.
-  // TODO(dneto): Remove this at some point once fixed drivers are widely available.
+  // TODO(dneto): Remove this at some point once fixed drivers are widely
+  // available.
   if (WorkgroupSizeVarID) {
     assert(WorkgroupSizeValueID);
 
@@ -3522,7 +3537,7 @@
     }
   }
 
-  const std::map<unsigned, spv::Op> Map {
+  const std::map<unsigned, spv::Op> Map{
       {Instruction::Add, spv::OpIAdd},
       {Instruction::FAdd, spv::OpFAdd},
       {Instruction::Sub, spv::OpISub},
@@ -3565,7 +3580,6 @@
       // Generate SPIRV instructions for cast operators.
       //
 
-
       auto Ty = I.getType();
       auto OpTy = I.getOperand(0)->getType();
       auto toI8 = Ty == Type::getInt8Ty(Context);
@@ -3574,7 +3588,7 @@
       if ((I.getOpcode() == Instruction::ZExt ||
            I.getOpcode() == Instruction::SExt ||
            I.getOpcode() == Instruction::UIToFP) &&
-           OpTy->isIntOrIntVectorTy(1)) {
+          OpTy->isIntOrIntVectorTy(1)) {
         //
         // Generate OpSelect.
         //
@@ -3707,7 +3721,7 @@
     // Ops[2] ... Ops[n] = Indexes ID
     SPIRVOperandList Ops;
 
-    PointerType* ResultType = cast<PointerType>(GEP->getType());
+    PointerType *ResultType = cast<PointerType>(GEP->getType());
     if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate ||
         GlobalConstArgSet.count(GEP->getPointerOperand())) {
       // Use pointer type with private address space for global constant.
@@ -4005,7 +4019,8 @@
 
       // Create correct value according to index of insertelement.
       Ops.clear();
-      Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(1)]) << MkId(ShiftAmountID);
+      Ops << MkId(ResTyID) << MkId(VMap[I.getOperand(1)])
+          << MkId(ShiftAmountID);
 
       uint32_t InsertValID = nextID;
 
@@ -4036,15 +4051,13 @@
       // Ops[1] = Object ID
       // Ops[2] = Composite ID
       // Ops[3] ... Ops[n] = Indexes (Literal Number)
-      Ops << MkId(VMap[I.getOperand(1)])
-          << MkId(VMap[I.getOperand(0)])
+      Ops << MkId(VMap[I.getOperand(1)]) << MkId(VMap[I.getOperand(0)])
           << MkNum(static_cast<uint32_t>(value));
     } else {
       // Ops[1] = Composite ID
       // Ops[2] = Object ID
       // Ops[3] ... Ops[n] = Indexes (Literal Number)
-      Ops << MkId(VMap[I.getOperand(0)])
-          << MkId(VMap[I.getOperand(1)])
+      Ops << MkId(VMap[I.getOperand(0)]) << MkId(VMap[I.getOperand(1)])
           << MkId(VMap[I.getOperand(2)]);
       Opcode = spv::OpVectorInsertDynamic;
     }
@@ -4111,7 +4124,7 @@
     CmpInst *CmpI = cast<CmpInst>(&I);
 
     // Pointer equality is invalid.
-    Type* ArgTy = CmpI->getOperand(0)->getType();
+    Type *ArgTy = CmpI->getOperand(0)->getType();
     if (isa<PointerType>(ArgTy)) {
       CmpI->print(errs());
       std::string name = I.getParent()->getParent()->getName();
@@ -4336,10 +4349,12 @@
         // No code is generated for the call.
       }
       break;
-    } else if (Callee->getName().startswith(clspv::WorkgroupAccessorFunction())) {
+    } else if (Callee->getName().startswith(
+                   clspv::WorkgroupAccessorFunction())) {
       // Don't codegen an instruction here, but instead map this call directly
       // to the workgroup variable id.
-      int spec_id = static_cast<int>(cast<ConstantInt>(Call->getOperand(0))->getSExtValue());
+      int spec_id = static_cast<int>(
+          cast<ConstantInt>(Call->getOperand(0))->getSExtValue());
       const auto &info = LocalSpecIdInfoMap[spec_id];
       VMap[Call] = info.variable_id;
       break;
@@ -4357,7 +4372,8 @@
       const auto load_id = nextID++;
 
       Ops << MkId(lookupType(SamplerTy->getPointerElementType()))
-          << MkId(SamplerMapIndexToIDMap[static_cast<unsigned>(index_into_sampler_map)]);
+          << MkId(SamplerMapIndexToIDMap[static_cast<unsigned>(
+                 index_into_sampler_map)]);
 
       auto *Inst = new SPIRVInstruction(spv::OpLoad, load_id, Ops);
       SPIRVInstList.push_back(Inst);
@@ -5207,9 +5223,9 @@
     Type *elemTy = nullptr;
     if (auto *ptrTy = dyn_cast<PointerType>(type)) {
       elemTy = ptrTy->getElementType();
-    } else if (auto* arrayTy = dyn_cast<ArrayType>(type)) {
+    } else if (auto *arrayTy = dyn_cast<ArrayType>(type)) {
       elemTy = arrayTy->getArrayElementType();
-    } else if (auto* seqTy = dyn_cast<SequentialType>(type)) {
+    } else if (auto *seqTy = dyn_cast<SequentialType>(type)) {
       elemTy = seqTy->getSequentialElementType();
     } else {
       errs() << "Unhandled strided type " << *type << "\n";
@@ -5234,7 +5250,7 @@
   // Emit SpecId decorations targeting the array size value.
   for (auto spec_id = clspv::FirstLocalSpecId(); spec_id < max_local_spec_id_;
        ++spec_id) {
-    LocalArgInfo& arg_info = LocalSpecIdInfoMap[spec_id];
+    LocalArgInfo &arg_info = LocalSpecIdInfoMap[spec_id];
     SPIRVOperandList Ops;
     Ops << MkId(arg_info.array_size_id) << MkNum(spv::DecorationSpecId)
         << MkNum(arg_info.spec_id);
@@ -5384,7 +5400,8 @@
       .Default(kGlslExtInstBad);
 }
 
-glsl::ExtInst SPIRVProducerPass::getDirectOrIndirectExtInstEnum(StringRef Name) {
+glsl::ExtInst
+SPIRVProducerPass::getDirectOrIndirectExtInstEnum(StringRef Name) {
   auto direct = getExtInstEnum(Name);
   if (direct != kGlslExtInstBad)
     return direct;
@@ -5477,7 +5494,8 @@
 }
 
 void SPIRVProducerPass::PrintSourceLanguage(SPIRVOperand *Op) {
-  spv::SourceLanguage SourceLang = static_cast<spv::SourceLanguage>(Op->getNumID());
+  spv::SourceLanguage SourceLang =
+      static_cast<spv::SourceLanguage>(Op->getNumID());
   out << spv::getSourceLanguageName(SourceLang);
 }
 
@@ -6190,40 +6208,42 @@
   }
 }
 
-bool SPIRVProducerPass::IsTypeNullable(const Type* type) const {
+bool SPIRVProducerPass::IsTypeNullable(const Type *type) const {
   switch (type->getTypeID()) {
-    case Type::HalfTyID:
-    case Type::FloatTyID:
-    case Type::DoubleTyID:
-    case Type::IntegerTyID:
-    case Type::VectorTyID:
-      return true;
-    case Type::PointerTyID: {
-      const PointerType *pointer_type = cast<PointerType>(type);
-      if (pointer_type->getPointerAddressSpace() !=
-          AddressSpace::UniformConstant) {
-        auto pointee_type = pointer_type->getPointerElementType();
-        if (pointee_type->isStructTy() &&
-            cast<StructType>(pointee_type)->isOpaque()) {
-          // Images and samplers are not nullable.
-          return false;
-        }
+  case Type::HalfTyID:
+  case Type::FloatTyID:
+  case Type::DoubleTyID:
+  case Type::IntegerTyID:
+  case Type::VectorTyID:
+    return true;
+  case Type::PointerTyID: {
+    const PointerType *pointer_type = cast<PointerType>(type);
+    if (pointer_type->getPointerAddressSpace() !=
+        AddressSpace::UniformConstant) {
+      auto pointee_type = pointer_type->getPointerElementType();
+      if (pointee_type->isStructTy() &&
+          cast<StructType>(pointee_type)->isOpaque()) {
+        // Images and samplers are not nullable.
+        return false;
       }
-      return true;
     }
-    case Type::ArrayTyID:
-      return IsTypeNullable(cast<CompositeType>(type)->getTypeAtIndex(0u));
-    case Type::StructTyID: {
-      const StructType* struct_type = cast<StructType>(type);
-      // Images and samplers are not nullable.
-      if (struct_type->isOpaque()) return false;
-      for (const auto element : struct_type->elements()) {
-        if (!IsTypeNullable(element)) return false;
-      }
-      return true;
-    }
-    default:
+    return true;
+  }
+  case Type::ArrayTyID:
+    return IsTypeNullable(cast<CompositeType>(type)->getTypeAtIndex(0u));
+  case Type::StructTyID: {
+    const StructType *struct_type = cast<StructType>(type);
+    // Images and samplers are not nullable.
+    if (struct_type->isOpaque())
       return false;
+    for (const auto element : struct_type->elements()) {
+      if (!IsTypeNullable(element))
+        return false;
+    }
+    return true;
+  }
+  default:
+    return false;
   }
 }
 
@@ -6240,8 +6260,8 @@
       std::vector<uint32_t> offsets;
       for (const Metadata *offset_md : offset_vector->operands()) {
         const auto *constant_md = cast<ConstantAsMetadata>(offset_md);
-        offsets.push_back(
-            cast<ConstantInt>(constant_md->getValue())->getZExtValue());
+        offsets.push_back(static_cast<uint32_t>(
+            cast<ConstantInt>(constant_md->getValue())->getZExtValue()));
       }
       RemappedUBOTypeOffsets.insert(std::make_pair(type, offsets));
     }