LEGO: Spatial Accelerator Generation and Optimization for Tensor Applications