There's an interplay between math and physics, so it's like what came first, the chicken or the egg.
In the case of general relativity, Einstein was floundering around for how to mathematize it until he happened upon tensor analysis. Same thing for quantum mechanics; the Hilbert space was there first, and turned out to be an excellent fit.
Sometimes it goes the other way, e.g., physics first, as with Newton and Fourier.